annotate src/test/twice.cu @ 292:2bc63a22dd21

add twice
author ikkun
date Thu, 09 Feb 2017 19:51:32 +0900
parents src/test/main.cc@87128b876c63
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
290
625a19d81ed7 add Cmake
ikkun
parents:
diff changeset
1 #include <stdio.h>
625a19d81ed7 add Cmake
ikkun
parents:
diff changeset
2 #include <sys/time.h>
625a19d81ed7 add Cmake
ikkun
parents:
diff changeset
3 #include <string.h>
625a19d81ed7 add Cmake
ikkun
parents:
diff changeset
4 #include <stdlib.h>
625a19d81ed7 add Cmake
ikkun
parents:
diff changeset
5
292
2bc63a22dd21 add twice
ikkun
parents: 291
diff changeset
6 #include <cuda.h>
2bc63a22dd21 add twice
ikkun
parents: 291
diff changeset
7
291
87128b876c63 add test
ikkun
parents: 290
diff changeset
8 #include <cuda_runtime.h>
292
2bc63a22dd21 add twice
ikkun
parents: 291
diff changeset
9 #include "helper_cuda.h"
290
625a19d81ed7 add Cmake
ikkun
parents:
diff changeset
10
625a19d81ed7 add Cmake
ikkun
parents:
diff changeset
11 #define LENGTH (10)
625a19d81ed7 add Cmake
ikkun
parents:
diff changeset
12 #define THREAD (10)
625a19d81ed7 add Cmake
ikkun
parents:
diff changeset
13
625a19d81ed7 add Cmake
ikkun
parents:
diff changeset
14 double
625a19d81ed7 add Cmake
ikkun
parents:
diff changeset
15 getTime() {
625a19d81ed7 add Cmake
ikkun
parents:
diff changeset
16 struct timeval tv;
625a19d81ed7 add Cmake
ikkun
parents:
diff changeset
17 gettimeofday(&tv, NULL);
625a19d81ed7 add Cmake
ikkun
parents:
diff changeset
18 return tv.tv_sec + (double)tv.tv_usec*1e-6;
625a19d81ed7 add Cmake
ikkun
parents:
diff changeset
19 }
625a19d81ed7 add Cmake
ikkun
parents:
diff changeset
20
625a19d81ed7 add Cmake
ikkun
parents:
diff changeset
21 void
625a19d81ed7 add Cmake
ikkun
parents:
diff changeset
22 check_data(float* A, float B, float* C) {
625a19d81ed7 add Cmake
ikkun
parents:
diff changeset
23 for (int i=0; i<LENGTH*THREAD; i++) {
625a19d81ed7 add Cmake
ikkun
parents:
diff changeset
24 if (A[i]*B!=C[i]) {
625a19d81ed7 add Cmake
ikkun
parents:
diff changeset
25 puts("multiply failure.");
625a19d81ed7 add Cmake
ikkun
parents:
diff changeset
26 return;
625a19d81ed7 add Cmake
ikkun
parents:
diff changeset
27 }
625a19d81ed7 add Cmake
ikkun
parents:
diff changeset
28 }
625a19d81ed7 add Cmake
ikkun
parents:
diff changeset
29 puts("success.");
625a19d81ed7 add Cmake
ikkun
parents:
diff changeset
30 }
625a19d81ed7 add Cmake
ikkun
parents:
diff changeset
31
625a19d81ed7 add Cmake
ikkun
parents:
diff changeset
32 void print_result(float* C) {
625a19d81ed7 add Cmake
ikkun
parents:
diff changeset
33 for (int i=0; i<LENGTH*THREAD; i++) {
625a19d81ed7 add Cmake
ikkun
parents:
diff changeset
34 printf("%f\n",C[i]);
625a19d81ed7 add Cmake
ikkun
parents:
diff changeset
35 }
625a19d81ed7 add Cmake
ikkun
parents:
diff changeset
36 }
625a19d81ed7 add Cmake
ikkun
parents:
diff changeset
37
625a19d81ed7 add Cmake
ikkun
parents:
diff changeset
38 int main(int args, char* argv[]) {
625a19d81ed7 add Cmake
ikkun
parents:
diff changeset
39 int num_stream = 1; // number of stream
625a19d81ed7 add Cmake
ikkun
parents:
diff changeset
40 int num_exec = 16; // number of executed kernel
625a19d81ed7 add Cmake
ikkun
parents:
diff changeset
41
625a19d81ed7 add Cmake
ikkun
parents:
diff changeset
42 for (int i=1;argv[i];i++) {
625a19d81ed7 add Cmake
ikkun
parents:
diff changeset
43 if (strcmp(argv[i], "--stream") == 0 || strcmp(argv[i], "-s") == 0) {
625a19d81ed7 add Cmake
ikkun
parents:
diff changeset
44 num_stream = atoi(argv[++i]);
625a19d81ed7 add Cmake
ikkun
parents:
diff changeset
45 }
625a19d81ed7 add Cmake
ikkun
parents:
diff changeset
46 }
625a19d81ed7 add Cmake
ikkun
parents:
diff changeset
47
625a19d81ed7 add Cmake
ikkun
parents:
diff changeset
48 // initialize and load kernel
625a19d81ed7 add Cmake
ikkun
parents:
diff changeset
49 CUdevice device;
625a19d81ed7 add Cmake
ikkun
parents:
diff changeset
50 CUcontext context;
625a19d81ed7 add Cmake
ikkun
parents:
diff changeset
51 CUmodule module;
625a19d81ed7 add Cmake
ikkun
parents:
diff changeset
52 CUfunction function;
625a19d81ed7 add Cmake
ikkun
parents:
diff changeset
53 CUstream stream[num_stream];
625a19d81ed7 add Cmake
ikkun
parents:
diff changeset
54
292
2bc63a22dd21 add twice
ikkun
parents: 291
diff changeset
55 checkCudaErrors(cuInit(0));
2bc63a22dd21 add twice
ikkun
parents: 291
diff changeset
56 checkCudaErrors(cuDeviceGet(&device, 0));
2bc63a22dd21 add twice
ikkun
parents: 291
diff changeset
57 checkCudaErrors(cuCtxCreate(&context, CU_CTX_SCHED_SPIN, device));
2bc63a22dd21 add twice
ikkun
parents: 291
diff changeset
58 checkCudaErrors(cuModuleLoad(&module, "multiply.ptx"));
2bc63a22dd21 add twice
ikkun
parents: 291
diff changeset
59 checkCudaErrors(cuModuleGetFunction(&function, module, "multiply"));
290
625a19d81ed7 add Cmake
ikkun
parents:
diff changeset
60 for (int i=0;i<num_stream;i++)
292
2bc63a22dd21 add twice
ikkun
parents: 291
diff changeset
61 checkCudaErrors(cuStreamCreate(&stream[i],0));
290
625a19d81ed7 add Cmake
ikkun
parents:
diff changeset
62
625a19d81ed7 add Cmake
ikkun
parents:
diff changeset
63 // memory allocate
625a19d81ed7 add Cmake
ikkun
parents:
diff changeset
64 CUdeviceptr devA;
625a19d81ed7 add Cmake
ikkun
parents:
diff changeset
65 CUdeviceptr devB[num_exec];
625a19d81ed7 add Cmake
ikkun
parents:
diff changeset
66 CUdeviceptr devOut[num_exec];
625a19d81ed7 add Cmake
ikkun
parents:
diff changeset
67
292
2bc63a22dd21 add twice
ikkun
parents: 291
diff changeset
68 checkCudaErrors(cuMemAlloc(&devA, LENGTH*THREAD*sizeof(float)));
290
625a19d81ed7 add Cmake
ikkun
parents:
diff changeset
69 for (int i=0;i<num_exec;i++) {
292
2bc63a22dd21 add twice
ikkun
parents: 291
diff changeset
70 checkCudaErrors(cuMemAlloc(&devB[i], sizeof(float)));
2bc63a22dd21 add twice
ikkun
parents: 291
diff changeset
71 checkCudaErrors(cuMemAlloc(&devOut[i], LENGTH*THREAD*sizeof(float)));
290
625a19d81ed7 add Cmake
ikkun
parents:
diff changeset
72 }
625a19d81ed7 add Cmake
ikkun
parents:
diff changeset
73
625a19d81ed7 add Cmake
ikkun
parents:
diff changeset
74 // input buffer
625a19d81ed7 add Cmake
ikkun
parents:
diff changeset
75 float* A = new float[LENGTH*THREAD];
625a19d81ed7 add Cmake
ikkun
parents:
diff changeset
76 float* B = new float[num_exec];
625a19d81ed7 add Cmake
ikkun
parents:
diff changeset
77
625a19d81ed7 add Cmake
ikkun
parents:
diff changeset
78 for (int i=0; i<LENGTH*THREAD; i++)
625a19d81ed7 add Cmake
ikkun
parents:
diff changeset
79 A[i] = (float)(i+1000);
625a19d81ed7 add Cmake
ikkun
parents:
diff changeset
80
625a19d81ed7 add Cmake
ikkun
parents:
diff changeset
81 // output buffer
625a19d81ed7 add Cmake
ikkun
parents:
diff changeset
82 float** result = new float* [num_exec];
625a19d81ed7 add Cmake
ikkun
parents:
diff changeset
83
625a19d81ed7 add Cmake
ikkun
parents:
diff changeset
84 for (int i=0;i<num_exec;i++)
625a19d81ed7 add Cmake
ikkun
parents:
diff changeset
85 result[i] = new float[LENGTH*THREAD];
625a19d81ed7 add Cmake
ikkun
parents:
diff changeset
86
625a19d81ed7 add Cmake
ikkun
parents:
diff changeset
87 // Synchronous data transfer(host to device)
292
2bc63a22dd21 add twice
ikkun
parents: 291
diff changeset
88 checkCudaErrors(cuMemcpyHtoD(devA, A, LENGTH*THREAD*sizeof(float)));
290
625a19d81ed7 add Cmake
ikkun
parents:
diff changeset
89
625a19d81ed7 add Cmake
ikkun
parents:
diff changeset
90 // Asynchronous data transfer(host to device)
625a19d81ed7 add Cmake
ikkun
parents:
diff changeset
91 int cur = 0;
625a19d81ed7 add Cmake
ikkun
parents:
diff changeset
92
625a19d81ed7 add Cmake
ikkun
parents:
diff changeset
93 for (int i=0;i<num_exec;i++,cur++) {
625a19d81ed7 add Cmake
ikkun
parents:
diff changeset
94 if (num_stream <= cur)
625a19d81ed7 add Cmake
ikkun
parents:
diff changeset
95 cur = 0;
625a19d81ed7 add Cmake
ikkun
parents:
diff changeset
96 B[i] = (float)(i+1);
292
2bc63a22dd21 add twice
ikkun
parents: 291
diff changeset
97 checkCudaErrors(cuMemcpyHtoDAsync(devB[i], &B[i], sizeof(float), stream[cur]));
290
625a19d81ed7 add Cmake
ikkun
parents:
diff changeset
98 }
625a19d81ed7 add Cmake
ikkun
parents:
diff changeset
99
625a19d81ed7 add Cmake
ikkun
parents:
diff changeset
100 cur = 0;
625a19d81ed7 add Cmake
ikkun
parents:
diff changeset
101
625a19d81ed7 add Cmake
ikkun
parents:
diff changeset
102 // Asynchronous launch kernel
625a19d81ed7 add Cmake
ikkun
parents:
diff changeset
103 for (int i=0;i<num_exec;i++,cur++) {
625a19d81ed7 add Cmake
ikkun
parents:
diff changeset
104 if (num_stream <= cur)
625a19d81ed7 add Cmake
ikkun
parents:
diff changeset
105 cur=0;
625a19d81ed7 add Cmake
ikkun
parents:
diff changeset
106 B[i] = (float)(i+1);
625a19d81ed7 add Cmake
ikkun
parents:
diff changeset
107 //cuMemcpyHtoDAsync(devB[i], &B[i], sizeof(float), stream[cur]);
625a19d81ed7 add Cmake
ikkun
parents:
diff changeset
108 void* args[] = {&devA, &devB[i], &devOut[i]};
292
2bc63a22dd21 add twice
ikkun
parents: 291
diff changeset
109 checkCudaErrors(cuLaunchKernel(function,
290
625a19d81ed7 add Cmake
ikkun
parents:
diff changeset
110 LENGTH, 1, 1,
625a19d81ed7 add Cmake
ikkun
parents:
diff changeset
111 THREAD, 1, 1,
625a19d81ed7 add Cmake
ikkun
parents:
diff changeset
112 0, stream[cur], args, NULL));
625a19d81ed7 add Cmake
ikkun
parents:
diff changeset
113 //cuMemcpyDtoHAsync(result[i], devOut[i], LENGTH*THREAD*sizeof(float), stream[cur]);
625a19d81ed7 add Cmake
ikkun
parents:
diff changeset
114 }
625a19d81ed7 add Cmake
ikkun
parents:
diff changeset
115
625a19d81ed7 add Cmake
ikkun
parents:
diff changeset
116 cur = 0;
625a19d81ed7 add Cmake
ikkun
parents:
diff changeset
117
625a19d81ed7 add Cmake
ikkun
parents:
diff changeset
118
625a19d81ed7 add Cmake
ikkun
parents:
diff changeset
119 // Asynchronous data transfer(device to host)
625a19d81ed7 add Cmake
ikkun
parents:
diff changeset
120 for (int i=0;i<num_exec;i++,cur++) {
625a19d81ed7 add Cmake
ikkun
parents:
diff changeset
121 if (num_stream <= cur)
625a19d81ed7 add Cmake
ikkun
parents:
diff changeset
122 cur = 0;
292
2bc63a22dd21 add twice
ikkun
parents: 291
diff changeset
123 checkCudaErrors(cuMemcpyDtoHAsync(result[i], devOut[i], LENGTH*THREAD*sizeof(float), stream[cur]));
290
625a19d81ed7 add Cmake
ikkun
parents:
diff changeset
124 }
625a19d81ed7 add Cmake
ikkun
parents:
diff changeset
125
625a19d81ed7 add Cmake
ikkun
parents:
diff changeset
126 // wait for stream
625a19d81ed7 add Cmake
ikkun
parents:
diff changeset
127 for (int i=0;i<num_stream;i++)
292
2bc63a22dd21 add twice
ikkun
parents: 291
diff changeset
128 checkCudaErrors(cuStreamSynchronize(stream[i]));
290
625a19d81ed7 add Cmake
ikkun
parents:
diff changeset
129
625a19d81ed7 add Cmake
ikkun
parents:
diff changeset
130 //printf("%0.6f\n",getTime()-start);
625a19d81ed7 add Cmake
ikkun
parents:
diff changeset
131
625a19d81ed7 add Cmake
ikkun
parents:
diff changeset
132 for (int i=0;i<num_exec;i++)
625a19d81ed7 add Cmake
ikkun
parents:
diff changeset
133 check_data(A,(float)(i+1),result[i]);
625a19d81ed7 add Cmake
ikkun
parents:
diff changeset
134
625a19d81ed7 add Cmake
ikkun
parents:
diff changeset
135 // memory release
292
2bc63a22dd21 add twice
ikkun
parents: 291
diff changeset
136 checkCudaErrors(cuMemFree(devA));
290
625a19d81ed7 add Cmake
ikkun
parents:
diff changeset
137 for (int i=0;i<num_exec;i++) {
292
2bc63a22dd21 add twice
ikkun
parents: 291
diff changeset
138 checkCudaErrors(cuMemFree(devB[i]));
2bc63a22dd21 add twice
ikkun
parents: 291
diff changeset
139 checkCudaErrors(cuMemFree(devOut[i]));
290
625a19d81ed7 add Cmake
ikkun
parents:
diff changeset
140 }
625a19d81ed7 add Cmake
ikkun
parents:
diff changeset
141 for (int i=0;i<num_stream;i++)
292
2bc63a22dd21 add twice
ikkun
parents: 291
diff changeset
142 checkCudaErrors(cuStreamDestroy(stream[i]));
2bc63a22dd21 add twice
ikkun
parents: 291
diff changeset
143 checkCudaErrors(cuModuleUnload(module));
2bc63a22dd21 add twice
ikkun
parents: 291
diff changeset
144 checkCudaErrors(cuCtxDestroy(context));
290
625a19d81ed7 add Cmake
ikkun
parents:
diff changeset
145
625a19d81ed7 add Cmake
ikkun
parents:
diff changeset
146 delete[] A;
625a19d81ed7 add Cmake
ikkun
parents:
diff changeset
147 delete[] B;
625a19d81ed7 add Cmake
ikkun
parents:
diff changeset
148 for (int i=0;i<num_exec;i++)
625a19d81ed7 add Cmake
ikkun
parents:
diff changeset
149 delete[] result[i];
625a19d81ed7 add Cmake
ikkun
parents:
diff changeset
150 delete[] result;
625a19d81ed7 add Cmake
ikkun
parents:
diff changeset
151
625a19d81ed7 add Cmake
ikkun
parents:
diff changeset
152 return 0;
625a19d81ed7 add Cmake
ikkun
parents:
diff changeset
153 }
625a19d81ed7 add Cmake
ikkun
parents:
diff changeset
154 //