def test_log10(self): """tests if the log function works""" a = simplearray.array(test_sample).fill_arange()+1 b = cumath.log10(a) for i in range(test_sample): self.assert_(abs(math.log10(a[i]) - b[i]) < 1e-3)
def test_fmod(self): """tests if the fmod function works""" a = simplearray.array(test_sample).fill_arange()/10 b = cumath.fmod(a,2) for i in range(test_sample): self.assert_(math.fmod(a[i],2) == b[i])
def test_exp(self): """tests if the exp function works""" a = simplearray.array(100).fill_arange()/10 b = cumath.exp(a) for i in range(100): self.assert_(abs(math.exp(a[i]) - b[i]) < 1e-2)
def test_ldexp(self): """tests if the ldexp function works""" a = simplearray.array(test_sample).fill_arange() b = cumath.ldexp(a,5) for i in range(test_sample): self.assert_(math.ldexp(a[i],5) == b[i])
def test_radians(self): """tests if the radians function works""" a = (simplearray.array(10).fill_arange()+1) b = cumath.radians(a) for i in range(10): self.assert_(abs(math.radians(a[i]) - b[i]) < 1e-2)
def test_floor(self): """tests if the floor function works""" a = simplearray.array(test_sample).fill_arange()/10 b = cumath.floor(a) for i in range(test_sample): self.assert_(math.floor(a[i]) == b[i])
def test_tanh(self): """tests if the tanh function works""" a = (simplearray.array(10).fill_arange()+1)/100 b = cumath.tanh(a) for i in range(10): self.assert_(abs(math.tanh(a[i]) - b[i]) < 1e-2)
def test_sqrt(self): """tests if the sqrt function works""" a = simplearray.array(10).fill_arange()+1 b = cumath.sqrt(a) for i in range(10): self.assert_(abs(math.sqrt(a[i]) - b[i]) < 1e-3)
def test_pow(self): """tests if the pow function works""" a = simplearray.array(10).fill_arange()+1 b = cumath.pow(a,2) for i in range(10): self.assert_(abs(math.pow(a[i],2) - b[i]) < 1e-3)
def test_fabs(self): """tests if the fabs function works""" a = simplearray.array(test_sample).fill_arange() * -1 b = cumath.fabs(a) for i in range(test_sample): self.assert_(a[i] + b[i] == 0) self.assert_(b[i] >= 0)
def test_frexp(self): """tests if the frexp function works""" a = simplearray.array(test_sample).fill_arange()/10 b = cumath.frexp(a) first = b[0] second = b[1] for i in range(test_sample): c = math.frexp(a[i]) self.assert_(c[0] == first[i]) self.assert_(c[1] == second[i])
def time_gpu_execution(size,method,argumentCount): """times the execution time on the gpu""" start = drv.Event() end = drv.Event() start.record() a = cuda.array(size)+1 for x in range(runs): if argumentCount == 1: method(a) if argumentCount == 2: method(a,2) #stop timer end.record() end.synchronize() #calculate used time secs = start.time_till(end) return secs
def find_pi_cuda(n): '''Takes in integer n (n points used for integration) ''' # here's Monte Carlo for finding pi seed = rand.seed_getter_unique(n) x = rand.XORWOWRandomNumberGenerator(seed, 0) y = rand.XORWOWRandomNumberGenerator(seed, 0) ones = gpuarray.ones( int(n)) # need an array the size of guesses to use mask distance = np.hypot(x, y) mask = np.where(distance <= 1.0) pi = 4 * np.sum(ones[mask]) / n error = np.abs(np.pi - pi) return pi, error start = time.time() test = np.linspace(0, 20, 21) test = 2**test.astype(int) error2 = gpuarray.array([]) for i in test: pi, error = find_pi_cuda(i) error2 = np.append(error2, error) stop = time.time() print("Time to run:", stop - start, "seconds; pi is", pi, "and error is", error)
#simple module to show the ploting of random data import pycuda.gpuarray as cuda from matplotlib.pylab import * size = 1000 #random data generated on gpu a = cuda.array(size).randn() subplot(211) plot(a) grid(True) ylabel('plot - gpu') subplot(212) hist(a, 100) grid(True) ylabel('histogram - gpu') #and save it savefig('plot-random-data')