File indexing completed on 2024-04-06 12:11:56
0001 #include <iostream>
0002 #include <stdlib.h>
0003 #include "oneapi/tbb/global_control.h"
0004 #include "oneapi/tbb/parallel_for.h"
0005 #include "oneapi/tbb/blocked_range.h"
0006
0007 using namespace tbb;
0008 using namespace std;
0009
0010 class ArraySummer {
0011 int* p_array_a;
0012 int* p_array_b;
0013 int* p_array_sum;
0014
0015 public:
0016
0017 ArraySummer(int* p_a, int* p_b, int* p_sum) : p_array_a(p_a), p_array_b(p_b), p_array_sum(p_sum) {}
0018
0019 void operator()(const blocked_range<int>& r) const {
0020 for (int i = r.begin(); i != r.end(); i++) {
0021 p_array_sum[i] = p_array_a[i] + p_array_b[i];
0022 }
0023 }
0024 };
0025
0026 int main(int argc, char* argv[]) {
0027 int* p_A;
0028 int* p_B;
0029 int* p_SUM_1T;
0030 int* p_SUM_TBB;
0031
0032
0033 global_control control(global_control::max_allowed_parallelism, 4);
0034
0035 constexpr int nElements = 10;
0036
0037 p_A = new int[nElements];
0038 p_B = new int[nElements];
0039 p_SUM_1T = new int[nElements];
0040 p_SUM_TBB = new int[nElements];
0041
0042
0043
0044
0045
0046 p_A[0] = p_B[0] = 0;
0047 p_A[1] = p_B[1] = 1;
0048 for (int i = 2; i < nElements; i++) {
0049 p_A[i] = (p_A[i - 1] + p_A[i - 2]) % (INT_MAX / 2);
0050 p_B[i] = p_A[i];
0051 p_SUM_1T[i] = 0;
0052 p_SUM_TBB[i] = 0;
0053 }
0054
0055
0056
0057
0058 for (int i = 0; i < nElements; i++) {
0059 p_SUM_1T[i] = p_A[i] + p_B[i];
0060 }
0061
0062
0063
0064
0065
0066 parallel_for(blocked_range<int>(0, nElements, 100), ArraySummer(p_A, p_B, p_SUM_TBB));
0067
0068
0069
0070
0071 for (int i = 0; i < nElements; i++) {
0072 if (p_SUM_1T[i] != p_SUM_TBB[i]) {
0073 cout << p_A[i] << " + " << p_B[i] << " = " << p_SUM_1T[i] << " AND " << p_SUM_TBB[i] << endl;
0074 }
0075 }
0076
0077 delete[] p_A;
0078 delete[] p_B;
0079 delete[] p_SUM_1T;
0080 delete[] p_SUM_TBB;
0081
0082 return 0;
0083 }