This repository has been archived by the owner on Feb 5, 2020. It is now read-only.
forked from aaswenson/FinalProject_759
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cuda_prep.hpp
112 lines (100 loc) · 3.13 KB
/
cuda_prep.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
// Allocate memory for ptrac data
particleTrack AllocatePtracData(particleTrack hdata){
cudaError_t error;
particleTrack data = hdata;
int size = hdata.Ntracks * sizeof(float);
error = cudaMalloc((void**)&data.x_pos, size);
error = cudaMalloc((void**)&data.y_pos, size);
error = cudaMalloc((void**)&data.z_pos, size);
error = cudaMalloc((void**)&data.u, size);
error = cudaMalloc((void**)&data.v, size);
error = cudaMalloc((void**)&data.w, size);
error = cudaMalloc((void**)&data.track_length, size);
if (error != cudaSuccess)
{
printf("cudaMalloc returned error code %d, line(%d)\n", error, __LINE__);
exit(EXIT_FAILURE);
}
return data;
}
// Allocate memory for mesh data
twoDmesh AllocateMeshData(twoDmesh hmesh){
cudaError_t error;
twoDmesh dmesh = hmesh;
int size = (hmesh.N+1) * sizeof(float);
int sizeflux = hmesh.N*hmesh.N*hmesh.N*sizeof(float);
error = cudaMalloc((void**)&dmesh.x, size);
cudaMalloc((void**)&dmesh.y, size);
cudaMalloc((void**)&dmesh.z, size);
cudaMalloc((void**)&dmesh.flux, sizeflux);
if (error != cudaSuccess)
{
printf("cudaMalloc returned error code %d, line(%d)\n", error, __LINE__);
exit(EXIT_FAILURE);
}
return dmesh;
}
// copy the ptrac and mesh data to the device
void CopyDatatoDevice(particleTrack ddata, particleTrack hdata,
twoDmesh dmesh, twoDmesh hmesh)
{
cudaError_t error;
unsigned int size = hdata.Ntracks * sizeof(float);
unsigned int meshsize = (hmesh.N+1) * sizeof(float);
unsigned int sizeflux = hmesh.N*hmesh.N*hmesh.N*sizeof(float);
// copy all data to device
// Ptrac Data
error = cudaMemcpy(ddata.x_pos, hdata.x_pos, size,
cudaMemcpyHostToDevice);
cudaMemcpy(ddata.y_pos, hdata.y_pos, size,
cudaMemcpyHostToDevice);
cudaMemcpy(ddata.z_pos, hdata.z_pos, size,
cudaMemcpyHostToDevice);
cudaMemcpy(ddata.u, hdata.u, size,
cudaMemcpyHostToDevice);
cudaMemcpy(ddata.v, hdata.v, size,
cudaMemcpyHostToDevice);
cudaMemcpy(ddata.w, hdata.w, size,
cudaMemcpyHostToDevice);
cudaMemcpy(ddata.track_length, hdata.track_length, size,
cudaMemcpyHostToDevice);
// Mesh Data
cudaMemcpy(dmesh.x, hmesh.x, meshsize,
cudaMemcpyHostToDevice);
cudaMemcpy(dmesh.y, hmesh.y, meshsize,
cudaMemcpyHostToDevice);
cudaMemcpy(dmesh.z, hmesh.z, meshsize,
cudaMemcpyHostToDevice);
cudaMemcpy(dmesh.flux, hmesh.flux, sizeflux,
cudaMemcpyHostToDevice);
if (error != cudaSuccess)
{
printf("cudaMemcpy returned error code %d, line(%d)\n", error, __LINE__);
exit(EXIT_FAILURE);
}
}
void free_dev_mem(twoDmesh dmesh, particleTrack ddata){
cudaFree(dmesh.flux);
cudaFree(dmesh.x);
cudaFree(dmesh.y);
cudaFree(dmesh.z);
cudaFree(ddata.x_pos);
cudaFree(ddata.y_pos);
cudaFree(ddata.z_pos);
cudaFree(ddata.u);
cudaFree(ddata.v);
cudaFree(ddata.w);
cudaFree(ddata.track_length);
cudaFree(dmesh.flux);
}
//compare the data stored in two arrays on the host
float CompareResults(float* A, float* B, int elements, int Np)
{
float sum=0;
float error=0;
for(unsigned int i = 0; i < elements; i++){
error += A[i] - B[i];
sum += A[i];
}
return sum;
}