MCPcopy Create free account
hub / github.com/arrayfire/arrayfire / main

Function main

examples/getting_started/rainfall.cpp:30–71  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

28using namespace af;
29
30int main(int argc, char **argv) {
31 try {
32 int device = argc > 1 ? atoi(argv[1]) : 0;
33 af::setDevice(device);
34 af::info();
35
36 int days = 9, sites = 4;
37 int n = 10; // measurements
38 float day_[] = {0, 0, 1, 2, 5, 5, 6, 6, 7, 8}; // ascending
39 float site_[] = {2, 3, 0, 1, 1, 2, 0, 1, 2, 1};
40 float measurement_[] = {9, 5, 6, 3, 3, 8, 2, 6, 5, 10}; // inches
41 array day(n, day_);
42 array site(n, site_);
43 array measurement(n, measurement_);
44
45 array rainfall = constant(0, sites);
46 gfor(seq s, sites) { rainfall(s) = sum(measurement * (site == s)); }
47
48 printf("total rainfall at each site:\n");
49 af_print(rainfall);
50
51 array is_between = 1 <= day && day <= 5; // days 1 and 5
52 float rain_between = sum<float>(measurement * is_between);
53 printf("rain between days: %g\n", rain_between);
54
55 printf("number of days with rain: %g\n",
56 sum<float>(diff1(day) > 0) + 1);
57
58 array per_day = constant(0, days);
59 gfor(seq d, days) per_day(d) = sum(measurement * (day == d));
60
61 printf("total rainfall each day:\n");
62 af_print(per_day);
63
64 printf("number of days over five: %g\n", sum<float>(per_day > 5));
65 } catch (af::exception &e) {
66 fprintf(stderr, "%s\n", e.what());
67 throw;
68 }
69
70 return 0;
71}

Callers

nothing calls this directly

Calls 5

infoFunction · 0.85
constantFunction · 0.85
setDeviceFunction · 0.50
diff1Function · 0.50
sumFunction · 0.50

Tested by

no test coverage detected