annotate example/Cuda/main.cc @ 1954:30b95f372a0d draft

fix read_size
author masa
date Thu, 06 Feb 2014 20:08:08 +0900
parents 67e50779feb4
children a68dbdf9b429
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1918
15e8c50ed570 add cuda sample, not running
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1 #include <stdio.h>
15e8c50ed570 add cuda sample, not running
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
diff changeset
2
15e8c50ed570 add cuda sample, not running
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
diff changeset
3 #include <cuda.h>
15e8c50ed570 add cuda sample, not running
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
diff changeset
4
15e8c50ed570 add cuda sample, not running
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
diff changeset
5 #define LENGTH 1000
15e8c50ed570 add cuda sample, not running
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
diff changeset
6
15e8c50ed570 add cuda sample, not running
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
diff changeset
7 void check_data(float* A,float* B,float* C) {
15e8c50ed570 add cuda sample, not running
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
diff changeset
8 for (int i=0; i<LENGTH; i++) {
15e8c50ed570 add cuda sample, not running
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
diff changeset
9 if (A[i]*B[i]!=C[i]) {
15e8c50ed570 add cuda sample, not running
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
diff changeset
10 puts("failure.");
15e8c50ed570 add cuda sample, not running
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
diff changeset
11 return;
15e8c50ed570 add cuda sample, not running
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
diff changeset
12 }
15e8c50ed570 add cuda sample, not running
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
diff changeset
13 }
15e8c50ed570 add cuda sample, not running
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
diff changeset
14 puts("success.");
15e8c50ed570 add cuda sample, not running
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
diff changeset
15 return;
15e8c50ed570 add cuda sample, not running
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
diff changeset
16 }
15e8c50ed570 add cuda sample, not running
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
diff changeset
17
1919
d6e033734c12 running cuda sample
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents: 1918
diff changeset
18 void print_result(float* C) {
d6e033734c12 running cuda sample
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents: 1918
diff changeset
19 for (int i=0; i<LENGTH; i++) {
d6e033734c12 running cuda sample
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents: 1918
diff changeset
20 printf("%f\n",C[i]);
d6e033734c12 running cuda sample
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents: 1918
diff changeset
21 }
d6e033734c12 running cuda sample
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents: 1918
diff changeset
22 }
1918
15e8c50ed570 add cuda sample, not running
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
diff changeset
23
15e8c50ed570 add cuda sample, not running
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
diff changeset
24 int main() {
15e8c50ed570 add cuda sample, not running
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
diff changeset
25 CUdevice device;
15e8c50ed570 add cuda sample, not running
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
diff changeset
26 CUcontext context;
15e8c50ed570 add cuda sample, not running
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
diff changeset
27 CUmodule module;
15e8c50ed570 add cuda sample, not running
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
diff changeset
28 CUfunction function;
15e8c50ed570 add cuda sample, not running
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
diff changeset
29
15e8c50ed570 add cuda sample, not running
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
diff changeset
30 cuInit(0);
15e8c50ed570 add cuda sample, not running
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
diff changeset
31 cuDeviceGet(&device, 0);
15e8c50ed570 add cuda sample, not running
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
diff changeset
32 cuCtxCreate(&context, 0, device);
1919
d6e033734c12 running cuda sample
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents: 1918
diff changeset
33 cuModuleLoad(&module, "multiply.ptx");
1918
15e8c50ed570 add cuda sample, not running
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
diff changeset
34 cuModuleGetFunction(&function, module, "multiply");
1919
d6e033734c12 running cuda sample
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents: 1918
diff changeset
35
1935
67e50779feb4 CudaScheduler is runnig.
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents: 1925
diff changeset
36 CUresult ret;
67e50779feb4 CudaScheduler is runnig.
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents: 1925
diff changeset
37 int size = 8;
67e50779feb4 CudaScheduler is runnig.
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents: 1925
diff changeset
38 CUstream stream1[size];
67e50779feb4 CudaScheduler is runnig.
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents: 1925
diff changeset
39
67e50779feb4 CudaScheduler is runnig.
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents: 1925
diff changeset
40 for (int i=0;i<size;i++) {
67e50779feb4 CudaScheduler is runnig.
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents: 1925
diff changeset
41 ret=cuStreamCreate(&stream1[i],0);
67e50779feb4 CudaScheduler is runnig.
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents: 1925
diff changeset
42 }
67e50779feb4 CudaScheduler is runnig.
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents: 1925
diff changeset
43
67e50779feb4 CudaScheduler is runnig.
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents: 1925
diff changeset
44 printf("%d\n",ret);
1923
kkb
parents: 1922
diff changeset
45
kkb
parents: 1922
diff changeset
46
1918
15e8c50ed570 add cuda sample, not running
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
diff changeset
47 float* A = new float[LENGTH];
15e8c50ed570 add cuda sample, not running
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
diff changeset
48 float* B = new float[LENGTH];
15e8c50ed570 add cuda sample, not running
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
diff changeset
49 float* C = new float[LENGTH];
1919
d6e033734c12 running cuda sample
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents: 1918
diff changeset
50
1918
15e8c50ed570 add cuda sample, not running
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
diff changeset
51 for (int i=0; i<LENGTH; i++) {
15e8c50ed570 add cuda sample, not running
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
diff changeset
52 A[i] = (float)(i+1000);
15e8c50ed570 add cuda sample, not running
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
diff changeset
53 B[i] = (float)(i+1)/10.f;
15e8c50ed570 add cuda sample, not running
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
diff changeset
54 }
15e8c50ed570 add cuda sample, not running
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
diff changeset
55
15e8c50ed570 add cuda sample, not running
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
diff changeset
56 CUdeviceptr devA,devB,devC;
15e8c50ed570 add cuda sample, not running
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
diff changeset
57
15e8c50ed570 add cuda sample, not running
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
diff changeset
58 cuMemAlloc(&devA, LENGTH*sizeof(float));
15e8c50ed570 add cuda sample, not running
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
diff changeset
59 cuMemAlloc(&devB, LENGTH*sizeof(float));
15e8c50ed570 add cuda sample, not running
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
diff changeset
60 cuMemAlloc(&devC, LENGTH*sizeof(float));
15e8c50ed570 add cuda sample, not running
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
diff changeset
61
1935
67e50779feb4 CudaScheduler is runnig.
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents: 1925
diff changeset
62 cuMemcpyHtoDAsync(devA, A, LENGTH*sizeof(float), stream1[0]);
67e50779feb4 CudaScheduler is runnig.
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents: 1925
diff changeset
63 cuMemcpyHtoDAsync(devB, B, LENGTH*sizeof(float), stream1[0]);
67e50779feb4 CudaScheduler is runnig.
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents: 1925
diff changeset
64
1925
cd5bbd8ec5d6 fix CudaScheduler
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents: 1924
diff changeset
65 // void* args[] = {&devA, &devB, &devC};
1935
67e50779feb4 CudaScheduler is runnig.
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents: 1925
diff changeset
66 void** args=NULL;
67e50779feb4 CudaScheduler is runnig.
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents: 1925
diff changeset
67 // args=(void**)malloc(sizeof(void*)*8);
67e50779feb4 CudaScheduler is runnig.
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents: 1925
diff changeset
68 // args[0] = &devA;
67e50779feb4 CudaScheduler is runnig.
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents: 1925
diff changeset
69 // args[1] = &devB;
67e50779feb4 CudaScheduler is runnig.
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents: 1925
diff changeset
70 // args[2] = &devC;
1919
d6e033734c12 running cuda sample
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents: 1918
diff changeset
71
1935
67e50779feb4 CudaScheduler is runnig.
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents: 1925
diff changeset
72 ret=cuLaunchKernel(function,
67e50779feb4 CudaScheduler is runnig.
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents: 1925
diff changeset
73 LENGTH, 1, 1,
67e50779feb4 CudaScheduler is runnig.
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents: 1925
diff changeset
74 1, 1, 1,
67e50779feb4 CudaScheduler is runnig.
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents: 1925
diff changeset
75 0, stream1[0], args, NULL);
67e50779feb4 CudaScheduler is runnig.
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents: 1925
diff changeset
76 printf("%d\n",ret);
1919
d6e033734c12 running cuda sample
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents: 1918
diff changeset
77
1935
67e50779feb4 CudaScheduler is runnig.
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents: 1925
diff changeset
78 cuMemcpyDtoHAsync(C, devC, LENGTH*sizeof(float), stream1[0]);
1918
15e8c50ed570 add cuda sample, not running
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
diff changeset
79
1919
d6e033734c12 running cuda sample
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents: 1918
diff changeset
80 // print_result(C);
1918
15e8c50ed570 add cuda sample, not running
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
diff changeset
81 check_data(A, B, C);
15e8c50ed570 add cuda sample, not running
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
diff changeset
82
15e8c50ed570 add cuda sample, not running
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
diff changeset
83 delete[] A;
15e8c50ed570 add cuda sample, not running
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
diff changeset
84 delete[] B;
15e8c50ed570 add cuda sample, not running
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
diff changeset
85 delete[] C;
15e8c50ed570 add cuda sample, not running
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
diff changeset
86 cuMemFree(devA);
15e8c50ed570 add cuda sample, not running
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
diff changeset
87 cuMemFree(devB);
15e8c50ed570 add cuda sample, not running
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
diff changeset
88 cuMemFree(devC);
1919
d6e033734c12 running cuda sample
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents: 1918
diff changeset
89 cuModuleUnload(module);
1935
67e50779feb4 CudaScheduler is runnig.
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents: 1925
diff changeset
90 cuStreamDestroy(stream1[0]);
1919
d6e033734c12 running cuda sample
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents: 1918
diff changeset
91 cuCtxDestroy(context);
1918
15e8c50ed570 add cuda sample, not running
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
diff changeset
92
15e8c50ed570 add cuda sample, not running
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
diff changeset
93 return 0;
15e8c50ed570 add cuda sample, not running
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
diff changeset
94 }