Created
January 27, 2021 02:28
-
-
Save Mohammad-debug/b66e728cbeda7bd6ed722071d2f74ecd to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <cuda_runtime_api.h> | |
#include <stdio.h> | |
#include <thrust/host_vector.h> | |
#include <thrust/device_vector.h> | |
#include <iostream> | |
#define NZ 512// | |
#define NX 512// | |
using namespace std; | |
using real =double; | |
void allocate_array_2d(real**& preal, const int dim1, const int dim2) { | |
// Contiguous allocation of 2D arrays | |
preal = new real * [dim1]; | |
preal[0] = new real[dim1 * dim2]; | |
for (int i = 1; i < dim1; i++) preal[i] = preal[i - 1] + dim2; | |
for (int i = 0; i < dim1; i++) { | |
for (int j = 0; j < dim2; j++) { | |
preal[i][j] = 0; | |
} | |
} | |
} | |
#define cudaCheckError(code) \ | |
{ \ | |
if ((code) != cudaSuccess) { \ | |
fprintf(stderr, "Cuda failure %s:%d: '%s' \n", __FILE__, __LINE__, \ | |
cudaGetErrorString(code)); \ | |
} \ | |
} | |
int main() | |
{ | |
real** a; | |
std::cout.precision(30); | |
allocate_array_2d(a, NZ, NX);//input array | |
for (int i = 0; i < NZ; i++) { | |
for (int j = 0; j < NX; j++) { | |
a[i][j] = 2.14748e+09; | |
} | |
} | |
real* da; | |
cudaCheckError(cudaMalloc(&da, NZ * NX * sizeof(real))); | |
cudaCheckError(cudaMemcpy(da,a[0], NZ * NX * sizeof(real),cudaMemcpyHostToDevice)); | |
///************************ | |
//CUDA KERNELS ARE HERE | |
// REMOVED FOR CLEAR QUESTION | |
///************************* | |
real sum1=0; | |
thrust::device_ptr<real> dev_ptr = thrust::device_pointer_cast(da); | |
sum1 = thrust::reduce(dev_ptr, dev_ptr+NZ*NX, 0, thrust::plus<real>()); | |
cout<<" \nsum gpu "<< sum1<<"\n"; | |
real sum2=0; | |
////////CPU PART DOING SAME THING////// | |
for (int i = 0; i < NZ; i++) { | |
for (int j = 0; j < NX; j++) { | |
sum2 += a[i][j]; | |
} | |
} | |
cout<<"\nsum cpu "<< sum2<<"\n"; | |
if((sum2-sum1)<0.001) | |
std::cout << "\nSUCESS "<< "\n"; | |
else | |
std::cout << "\nFailure & by "<<sum2-sum1<< "\n"; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment