-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcuda-wh.cu
140 lines (111 loc) · 3.31 KB
/
cuda-wh.cu
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#include <iostream>
#include <math.h>
#include <fstream>
#include "cuwh.h"
using namespace std;
State * get_frame_ics(int Nx, int Ny, double thetaFOV, double phiFOV,
double cam_l, double cam_phi, double cam_theta)
{
int N = Nx*Ny;
State *ret = (State *)malloc(N*sizeof(State));
int i,j;
double pi = 3.14159;
double deg2rad = pi/180;
double mintheta = pi/2 - deg2rad*thetaFOV/2;
double maxtheta = pi/2 + deg2rad*thetaFOV/2;
double minphi = -deg2rad*phiFOV/2;
double maxphi = deg2rad*phiFOV/2;
double thetacs, phics;
double nx, ny, nz;
for(i=0; i<Nx; i++) // phi
{
for(j=0; j<Ny; j++) // theta
{
thetacs = mintheta + (((double) j)/Ny)*(maxtheta - mintheta);
phics = minphi + (((double) i)/Nx)*(maxphi - minphi);
nx = sin(thetacs)*cos(phics);
ny = sin(thetacs)*sin(phics);
nz = -1.0*cos(thetacs);
ret[i*Nx +j].r = cam_l;
ret[i*Nx +j].theta = cam_theta;
ret[i*Nx +j].phi = cam_phi;
ret[i*Nx +j].pr = nx;
ret[i*Nx +j].ptheta = cam_r*nz;
ret[i*Nx +j].b = cam_r*sin(cam_theta)*ny;
ret[i*Nx +j].Bsq = pow(cam_r, 2)*(pow(nz,2) + pow(ny,2));
}
}
return ret;
}
int compute_wh(State *ics, int Nx, int Ny)
{
State *states_device;
int N = Nx*Ny;
cudaError_t err = cudaMalloc((void**) &states_device, N*sizeof(State));
if (err != cudaSuccess) {
cout << "CUDA Memory Allocation Error" << endl;
return -1;
}
err = cudaMemcpy(states_device, &ics[0], N*sizeof(State), cudaMemcpyHostToDevice);
if (err != cudaSuccess) {
cout << "CUDA Memory Copy Error" << endl;
return -1;
}
int blockSize = Nx;
int numBlocks = N / blockSize;
// Integrate
double dt = 1e-3;
double t = 0.0;
double tend = 100.0;
int k = 0;
while(t < tend)
{
curk4(blockSize, numblocks, N, states_device, 1.0, dt);
t += dt;
k += 1;
if((k%1000) == 0)
{
cout << "Time: " << k*dt << endl;
}
}
// Retrieve Results
err = cudaMemcpy(states_host, states_device, N*sizeof(State), cudaMemcpyDeviceToHost);
if (err != cudaSuccess)
{
cout << "Retrieval error" << endl;
return -1;
}
cudaFree(states_device);
return 0;
}
int main(void)
{
int Nx = 512;
int Ny = 512; // Pixes
int N = Nx*Ny; // total pixels in image.
// Allocate Device array for initial conditions
State *states_device;
cudaError_t err = cudaMalloc((void**) &states_device, N*sizeof(State));
if (err != cudaSuccess)
{
cout << "Allocation error" << endl;
}
// Setup Initial conditions
double thetaFOV = 20;
double phiFOV = 20;
double cam_l = -10.0;
double cam_r = sqrt(1.0 + pow(cam_l, 2));
double cam_phi = 0.0;
double cam_theta = pi/2;
states_host = get_frame_ics(Nx, Ny, thetaFOV, phiFOV, cam_l, cam_phi, cam_theta);
int err = compute_wh(states_host, Nx, Ny);
if (err < 0) {
cout << "Computation Failed" << endl;
return -1;
}
ofstream outfile("mapout.dat", ios::binary);
outfile.write((char *)states_host, N*sizeof(State));
outfile.close();
free(states_host);
return 0;
}