b***@agnee.tezu.ernet.in
2016-07-12 15:04:48 UTC
1 i am actually solving pearson correlation coefficient (pcc)
2
3 import pycuda.driver as cuda
4 import pycuda.gpuarray as gpuarray
5 import pycuda.autoinit
6 from pycuda.compiler import SourceModule
7 import numpy as np
8 import time
9
10 start = time.time()
11 N = np.int32(512000)
12 block_size = 512
13 gridsize = N/block_size
14 x_cpu = np.random.randn(N).astype(np.float32)
15 x_gpu = gpuarray.to_gpu(x_cpu)
16
17 y_cpu = np.random.randn(N).astype(np.float32)
18 y_gpu = gpuarray.to_gpu(y_cpu)
19
20 #r = np.array(0).astype(np.float32)
21 res = gpuarray.empty_like(x_gpu)
22 xy_sum = gpuarray.sum(x_gpu * y_gpu)
23 x_sum = gpuarray.sum(x_gpu)
24 y_sum = gpuarray.sum(y_gpu)
25 sq_of_sum_of_x = gpuarray.sum(x_sum * x_sum)
26 #sq_of_sum_of_x = x_sum * y_sum # do in gpu
27 sq_of_sum_of_y = gpuarray.sum(y_sum * y_sum) # do in gpu
28 sum_of_ysq = gpuarray.sum(y_gpu * y_gpu)
29 sum_of_xsq = gpuarray.sum(x_gpu * x_gpu)
30 #res = gpuarray.sum(res1)
31 arr =
np.array([xy_sum,x_sum,y_sum,sq_of_sum_of_x,sq_of_sum_of_y,sum_of_ysq
,sum_of_xsq])
32 arr_gpu = gpuarray.to_gpu(arr)
33 print ("array=%s" %(arr))
# this is kernel1
34 '''kernels = SourceModule("""
35 __global__ void pcc(float *res,int N,float xy,float x,float y,float
sqsumx,float sqsumy,float sumysq, float sumxsq )'''
36
37 kernels = SourceModule("""
38 __global__ void pcc(float *res,int N, float *arr)
39 {
40 res[0] = arr[0];
41 // res[0] = 55;
42 // res[0]= (N * xy);// - (x * y)); //
43 // res[0] = sqrt((N * sqsumx - sumxsq)*(N * sqsumy - sumysq));
44 }
45 """)
46
47 cov = kernels.get_function("pcc")
48 cov(res,N,arr,block=(block_size,1,1),grid = (gridsize,1))
49 #####################################################################
#this second kernel
kernels = SourceModule("""
35 __global__ void pcc(float *res,int N,float xy,float x,float y,float
sqsumx,float sqsumy,float sumysq, float sumxsq )
39 {
40
41
42 res[0]= ((N * xy)- (x * y))/sqrt((N * sqsumx - sumxsq)*(N * sqsumy -
sumysq));
44 }
45 """)
cov = kernels.get_function("pcc")
cov(res,N,xy_sum,x_sum,y_sum,sq_of_sum_of_x,sq_of_sum_of_y,sum_of_ysq,sum_o
f_xsq,block=(block_size,1,1),grid = (gridsize,1))
50 ####################################################################
51 pcc = gpuarray.sum(res[0])
52 #pcc = gpuarray.sum((N * xy_sum - x_sum *
y_sum)/sqrt((N*sum_of_xsq-sq_of_su
m_of_x)*(N*sum_of_ysq-sq_of_sum_of_y)))
53 #print ("result of xy=%s" %(result))
54 print ("result of xy w/o kernal function = %s" %(xy_sum))
55 print ("result of x*x=%s" %(x_sum))
56 print ("result of y*y=%s" %(y_sum))
57 print ("result of sum of square of x=%s" %(sum_of_xsq))
58 print ("result of sum of square of y=%s" %(sum_of_ysq))
59 print ("result of sq_of_sum_of_x=%s" %(sq_of_sum_of_x))
60 print ("result of pcc=%s" %(pcc))
61 finish = time.time()
62 print("exection time =%s" %(finish-start))
please ignore the serial number
sir, if use kernel1 the i get error as
arr_gpu = gpuarray.to_gpu(arr)
File
"/opt/python/lib/python2.7/site-packages/pycuda-2016.1.1-py2.7-linux-x86_64.egg/pycuda/gpuarray.py",
line 976, in to_gpu
result.set(ary)
File
"/opt/python/lib/python2.7/site-packages/pycuda-2016.1.1-py2.7-linux-x86_64.egg/pycuda/gpuarray.py",
line 243, in set
_memcpy_discontig(self, ary, async=async, stream=stream)
File
"/opt/python/lib/python2.7/site-packages/pycuda-2016.1.1-py2.7-linux-x86_64.egg/pycuda/gpuarray.py",
line 1192, in _memcpy_discontig
src = _as_strided(src, shape=(src.size,), strides=(src.dtype.itemsize,))
File
"/opt/python/lib/python2.7/site-packages/numpy/lib/stride_tricks.py",
line 32, in as_strided
array.dtype = x.dtype
TypeError: Cannot change data-type for object array.
and if i use kernel 2 the i get answer as
res[0] = 0.0
i.e it does not value from ((N * xy)- (x * y))/sqrt((N * sqsumx -
sumxsq)*(N * sqsumy - sumysq));
can you please help me out sir.
-----------------------------------------
___________________
D I S C L A I M E R
This e-mail may contain privileged information and is intended solely for
the individual named. If you are not the named addressee you should not
disseminate, distribute or copy this e-mail. Please notify the sender
immediately by e-mail if you have received this e-mail in error and
destroy it from your system. Though considerable effort has been made to
deliver error free e-mail messages but it can not be guaranteed to be secure
or error-free as information could be intercepted, corrupted, lost, destroyed,
delayed or incomplete, or may contain viruses. The recipient must verify
the integrity of this e-mail message.
2
3 import pycuda.driver as cuda
4 import pycuda.gpuarray as gpuarray
5 import pycuda.autoinit
6 from pycuda.compiler import SourceModule
7 import numpy as np
8 import time
9
10 start = time.time()
11 N = np.int32(512000)
12 block_size = 512
13 gridsize = N/block_size
14 x_cpu = np.random.randn(N).astype(np.float32)
15 x_gpu = gpuarray.to_gpu(x_cpu)
16
17 y_cpu = np.random.randn(N).astype(np.float32)
18 y_gpu = gpuarray.to_gpu(y_cpu)
19
20 #r = np.array(0).astype(np.float32)
21 res = gpuarray.empty_like(x_gpu)
22 xy_sum = gpuarray.sum(x_gpu * y_gpu)
23 x_sum = gpuarray.sum(x_gpu)
24 y_sum = gpuarray.sum(y_gpu)
25 sq_of_sum_of_x = gpuarray.sum(x_sum * x_sum)
26 #sq_of_sum_of_x = x_sum * y_sum # do in gpu
27 sq_of_sum_of_y = gpuarray.sum(y_sum * y_sum) # do in gpu
28 sum_of_ysq = gpuarray.sum(y_gpu * y_gpu)
29 sum_of_xsq = gpuarray.sum(x_gpu * x_gpu)
30 #res = gpuarray.sum(res1)
31 arr =
np.array([xy_sum,x_sum,y_sum,sq_of_sum_of_x,sq_of_sum_of_y,sum_of_ysq
,sum_of_xsq])
32 arr_gpu = gpuarray.to_gpu(arr)
33 print ("array=%s" %(arr))
# this is kernel1
34 '''kernels = SourceModule("""
35 __global__ void pcc(float *res,int N,float xy,float x,float y,float
sqsumx,float sqsumy,float sumysq, float sumxsq )'''
36
37 kernels = SourceModule("""
38 __global__ void pcc(float *res,int N, float *arr)
39 {
40 res[0] = arr[0];
41 // res[0] = 55;
42 // res[0]= (N * xy);// - (x * y)); //
43 // res[0] = sqrt((N * sqsumx - sumxsq)*(N * sqsumy - sumysq));
44 }
45 """)
46
47 cov = kernels.get_function("pcc")
48 cov(res,N,arr,block=(block_size,1,1),grid = (gridsize,1))
49 #####################################################################
#this second kernel
kernels = SourceModule("""
35 __global__ void pcc(float *res,int N,float xy,float x,float y,float
sqsumx,float sqsumy,float sumysq, float sumxsq )
39 {
40
41
42 res[0]= ((N * xy)- (x * y))/sqrt((N * sqsumx - sumxsq)*(N * sqsumy -
sumysq));
44 }
45 """)
cov = kernels.get_function("pcc")
cov(res,N,xy_sum,x_sum,y_sum,sq_of_sum_of_x,sq_of_sum_of_y,sum_of_ysq,sum_o
f_xsq,block=(block_size,1,1),grid = (gridsize,1))
50 ####################################################################
51 pcc = gpuarray.sum(res[0])
52 #pcc = gpuarray.sum((N * xy_sum - x_sum *
y_sum)/sqrt((N*sum_of_xsq-sq_of_su
m_of_x)*(N*sum_of_ysq-sq_of_sum_of_y)))
53 #print ("result of xy=%s" %(result))
54 print ("result of xy w/o kernal function = %s" %(xy_sum))
55 print ("result of x*x=%s" %(x_sum))
56 print ("result of y*y=%s" %(y_sum))
57 print ("result of sum of square of x=%s" %(sum_of_xsq))
58 print ("result of sum of square of y=%s" %(sum_of_ysq))
59 print ("result of sq_of_sum_of_x=%s" %(sq_of_sum_of_x))
60 print ("result of pcc=%s" %(pcc))
61 finish = time.time()
62 print("exection time =%s" %(finish-start))
please ignore the serial number
sir, if use kernel1 the i get error as
arr_gpu = gpuarray.to_gpu(arr)
File
"/opt/python/lib/python2.7/site-packages/pycuda-2016.1.1-py2.7-linux-x86_64.egg/pycuda/gpuarray.py",
line 976, in to_gpu
result.set(ary)
File
"/opt/python/lib/python2.7/site-packages/pycuda-2016.1.1-py2.7-linux-x86_64.egg/pycuda/gpuarray.py",
line 243, in set
_memcpy_discontig(self, ary, async=async, stream=stream)
File
"/opt/python/lib/python2.7/site-packages/pycuda-2016.1.1-py2.7-linux-x86_64.egg/pycuda/gpuarray.py",
line 1192, in _memcpy_discontig
src = _as_strided(src, shape=(src.size,), strides=(src.dtype.itemsize,))
File
"/opt/python/lib/python2.7/site-packages/numpy/lib/stride_tricks.py",
line 32, in as_strided
array.dtype = x.dtype
TypeError: Cannot change data-type for object array.
and if i use kernel 2 the i get answer as
res[0] = 0.0
i.e it does not value from ((N * xy)- (x * y))/sqrt((N * sqsumx -
sumxsq)*(N * sqsumy - sumysq));
can you please help me out sir.
-----------------------------------------
___________________
D I S C L A I M E R
This e-mail may contain privileged information and is intended solely for
the individual named. If you are not the named addressee you should not
disseminate, distribute or copy this e-mail. Please notify the sender
immediately by e-mail if you have received this e-mail in error and
destroy it from your system. Though considerable effort has been made to
deliver error free e-mail messages but it can not be guaranteed to be secure
or error-free as information could be intercepted, corrupted, lost, destroyed,
delayed or incomplete, or may contain viruses. The recipient must verify
the integrity of this e-mail message.