Skip to content

Commit e11bc7a

Browse files
committed
add cuda exe sample
1 parent 098adbd commit e11bc7a

File tree

4 files changed

+37
-3
lines changed

4 files changed

+37
-3
lines changed

cudart32_80.dll

292 KB
Binary file not shown.

cudart64_80.dll

359 KB
Binary file not shown.

kernel.cu

+37-3
Original file line numberDiff line numberDiff line change
@@ -25,17 +25,23 @@ typedef struct Orb{
2525
double vy;
2626
double vz;
2727
double m;
28-
short st;
28+
double st;
2929
}Orb;
3030

3131
#define G 0.000005
3232
#define MIN_DIST 1.0
3333
const int unitSize = 8;//unitSize:每个计算单位的大小,多少个float
34-
double wide = 1000;
34+
double wide = 10000;
3535
double mass = 10;
3636
double velo = 0.005;
3737
int saveTimes = 0;
3838

39+
void calcGravity(Orb*o, Orb*ta, double dist, double*gx, double*gy, double*gz);
40+
void calcOne(Orb*o, int oId, Orb*olist, int nUnit);
41+
Orb* newOrbList(int nUnit, int style);
42+
void deleteOrbList(Orb *olist);
43+
void initOrbList(Orb *olist, int nUnit, int style);
44+
3945
/* calc gravity between two*/
4046
void calcGravity(Orb*o, Orb*ta, double dist, double*gx, double*gy, double*gz) {
4147
double a = ta->m / (dist*dist) * G;
@@ -82,7 +88,9 @@ Orb* newOrbList(int nUnit, int style) {
8288
}
8389

8490
void deleteOrbList(Orb *olist) {
85-
91+
if (olist != NULL) {
92+
delete(olist);
93+
}
8694
}
8795
void initOrbList(Orb *olist, int nUnit, int style) {
8896
int i = 0;
@@ -129,6 +137,32 @@ int main()
129137
return 0;
130138
}
131139

140+
cudaError_t calcOrbsWithCuda(Orb* olist, int nUnit, int nTimes) {
141+
int* dev_a = 0;
142+
int* dev_b = 0;
143+
cudaError_t cudaStatus;
144+
145+
// Choose which GPU to run on, change this on a multi-GPU system.
146+
cudaStatus = cudaSetDevice(0);
147+
if (cudaStatus != cudaSuccess) {
148+
fprintf(stderr, "cudaSetDevice failed! Do you have a CUDA-capable GPU installed?");
149+
goto Error;
150+
}
151+
152+
// Allocate GPU buffers for three vectors (two input, one output) .
153+
cudaStatus = cudaMalloc((void**)&dev_a, nUnit * sizeof(Orb));
154+
if (cudaStatus != cudaSuccess) {
155+
fprintf(stderr, "cudaMalloc failed!");
156+
goto Error;
157+
}
158+
159+
Error:
160+
cudaFree(dev_a);
161+
cudaFree(dev_b);
162+
163+
return cudaStatus;
164+
}
165+
132166
// Helper function for using CUDA to add vectors in parallel.
133167
cudaError_t addWithCuda(int *c, const int *a, const int *b, unsigned int size)
134168
{

thecuda1.exe

56.5 KB
Binary file not shown.

0 commit comments

Comments
 (0)