MCPcopy Create free account
hub / github.com/antmachineintelligence/mtgbmcode / main

Function main

compute/example/mapped_view.cpp:23–45  ·  view source on GitHub ↗

this example demonstrates how to use the mapped_view class to map an array of numbers to device memory and use the reduce() algorithm to calculate the sum.

Source from the content-addressed store, hash-verified

21// an array of numbers to device memory and use the reduce() algorithm
22// to calculate the sum.
23int main()
24{
25 // get default device and setup context
26 compute::device gpu = compute::system::default_device();
27 compute::context context(gpu);
28 compute::command_queue queue(context, gpu);
29 std::cout << "device: " << gpu.name() << std::endl;
30
31 // create data on host
32 int data[] = { 4, 2, 3, 7, 8, 9, 1, 6 };
33
34 // create mapped view on device
35 compute::mapped_view<int> view(data, 8, context);
36
37 // use reduce() to calculate sum on the device
38 int sum = 0;
39 compute::reduce(view.begin(), view.end(), &sum, queue);
40
41 // print the sum on the host
42 std::cout << "sum: " << sum << std::endl;
43
44 return 0;
45}

Callers

nothing calls this directly

Calls 4

reduceFunction · 0.85
nameMethod · 0.45
beginMethod · 0.45
endMethod · 0.45

Tested by

no test coverage detected