MCPcopy Create free account
hub / github.com/boostorg/compute / main

Function main

example/point_centroid.cpp:24–66  ·  view source on GitHub ↗

the point centroid example calculates and displays the centroid of a set of 3D points stored as float4's

Source from the content-addressed store, hash-verified

22// the point centroid example calculates and displays the
23// centroid of a set of 3D points stored as float4's
24int main()
25{
26 using compute::float4_;
27
28 // get default device and setup context
29 compute::device device = compute::system::default_device();
30 compute::context context(device);
31 compute::command_queue queue(context, device);
32
33 // point coordinates
34 float points[] = { 1.0f, 2.0f, 3.0f, 0.0f,
35 -2.0f, -3.0f, 4.0f, 0.0f,
36 1.0f, -2.0f, 2.5f, 0.0f,
37 -7.0f, -3.0f, -2.0f, 0.0f,
38 3.0f, 4.0f, -5.0f, 0.0f };
39
40 // create vector for five points
41 compute::vector<float4_> vector(5, context);
42
43 // copy point data to the device
44 compute::copy(
45 reinterpret_cast<float4_ *>(points),
46 reinterpret_cast<float4_ *>(points) + 5,
47 vector.begin(),
48 queue
49 );
50
51 // calculate sum
52 float4_ sum = compute::accumulate(
53 vector.begin(), vector.end(), float4_(0, 0, 0, 0), queue
54 );
55
56 // calculate centroid
57 float4_ centroid;
58 for(size_t i = 0; i < 3; i++){
59 centroid[i] = sum[i] / 5.0f;
60 }
61
62 // print centroid
63 std::cout << "centroid: " << centroid << std::endl;
64
65 return 0;
66}
67
68//]

Callers

nothing calls this directly

Calls 4

copyFunction · 0.85
accumulateFunction · 0.85
beginMethod · 0.45
endMethod · 0.45

Tested by

no test coverage detected