def maximum_cl(a, b=None): """ Maximum values of two GPUArrays. Parameters ---------- a : gpuarray First GPUArray. b : gpuarray Second GPUArray. Returns ------- gpuarray Maximum values from both GPArrays, or single value if one GPUarray. Examples -------- >>> a = maximum_cl(give_cl(queue, [1, 2, 3]), give_cl(queue, [3, 2, 1])) [3, 2, 3] >>> type(a) <class 'pyopencl.array.Array'> """ if b is not None: return cl_array.maximum(a, b) return cl_array.max(a)
def maximum(t: Tensor, uts: Union[Tensor, Scalar]) -> Tensor: """Returns the maximum of a tensor.""" if t.gpu: if not isinstance(uts, Tensor): ot: cl.array.Array = clarray.empty(QUEUE, t.shape, dtype=np.float32).fill(uts) return Tensor(clarray.maximum(t._data, ot), gpu=True) return Tensor(clarray.maximum(t._data, uts._data), gpu=True) if not isinstance(uts, Tensor): return Tensor(np.maximum(t._data, uts)) return Tensor(np.maximum(t._data, uts._data))
def test_if_positive(ctx_getter): context = ctx_getter() queue = cl.CommandQueue(context) from pyopencl.clrandom import rand as clrand l = 20000 a_gpu = clrand(context, queue, (l,), numpy.float32) b_gpu = clrand(context, queue, (l,), numpy.float32) a = a_gpu.get() b = b_gpu.get() max_a_b_gpu = cl_array.maximum(a_gpu, b_gpu) min_a_b_gpu = cl_array.minimum(a_gpu, b_gpu) print(max_a_b_gpu) print(numpy.maximum(a, b)) assert la.norm(max_a_b_gpu.get()- numpy.maximum(a, b)) == 0 assert la.norm(min_a_b_gpu.get()- numpy.minimum(a, b)) == 0
def test_if_positive(ctx_factory): context = ctx_factory() queue = cl.CommandQueue(context) from pyopencl.clrandom import rand as clrand ary_len = 20000 a_gpu = clrand(queue, (ary_len,), np.float32) b_gpu = clrand(queue, (ary_len,), np.float32) a = a_gpu.get() b = b_gpu.get() max_a_b_gpu = cl_array.maximum(a_gpu, b_gpu) min_a_b_gpu = cl_array.minimum(a_gpu, b_gpu) print(max_a_b_gpu) print(np.maximum(a, b)) assert la.norm(max_a_b_gpu.get() - np.maximum(a, b)) == 0 assert la.norm(min_a_b_gpu.get() - np.minimum(a, b)) == 0
def test_if_positive(ctx_getter): context = ctx_getter() queue = cl.CommandQueue(context) from pyopencl.clrandom import rand as clrand l = 20000 a_gpu = clrand(context, queue, (l, ), numpy.float32) b_gpu = clrand(context, queue, (l, ), numpy.float32) a = a_gpu.get() b = b_gpu.get() max_a_b_gpu = cl_array.maximum(a_gpu, b_gpu) min_a_b_gpu = cl_array.minimum(a_gpu, b_gpu) print max_a_b_gpu print numpy.maximum(a, b) assert la.norm(max_a_b_gpu.get() - numpy.maximum(a, b)) == 0 assert la.norm(min_a_b_gpu.get() - numpy.minimum(a, b)) == 0
def maximum(self, x, y): import pyopencl.array as cl_array return cl_array.maximum(x, y)
def _gpu_search(self): """Method that actually performs the exhaustive search on the GPU""" # make shortcuts d = self.data g = self.gpu_data q = self.queue k = g['k'] # initalize the total number of sampled complexes tot_complexes = cl_array.sum(g['interspace'], dtype=np.float32) # initialize time time0 = _time() # loop over all rotations for n in xrange(g['nrot']): # rotate the scanning chain object k.rotate_image3d(q, g['sampler'], g['im_lsurf'], self.rotations[n], g['lsurf'], d['im_center']) # perform the FFTs and calculate the clashing and interaction volume k.rfftn(q, g['lsurf'], g['ft_lsurf']) k.c_conj_multiply(q, g['ft_lsurf'], g['ft_rcore'], g['ft_clashvol']) k.irfftn(q, g['ft_clashvol'], g['clashvol']) k.c_conj_multiply(q, g['ft_lsurf'], g['ft_rsurf'], g['ft_intervol']) k.irfftn(q, g['ft_intervol'], g['intervol']) # determine at every position if the conformation is a proper complex k.touch(q, g['clashvol'], g['max_clash'], g['intervol'], g['min_interaction'], g['interspace']) if self.distance_restraints: k.fill(q, g['restspace'], 0) # determine the space that is consistent with a number of # distance restraints k.distance_restraint(q, g['restraints'], self.rotations[n], g['restspace']) # get the accessible interaction space also consistent with a # certain number of distance restraints k.multiply(q, g['restspace'], g['interspace'], g['access_interspace']) # calculate the total number of complexes, while taking into # account orientational/rotational bias tot_complexes += cl_array.sum(g['interspace'], dtype=np.float32) * np.float32( self.weights[n]) # take at every position in space the maximum number of consistent # restraints for later visualization cl_array.maximum(g['best_access_interspace'], g['access_interspace'], g['best_access_interspace']) # calculate the number of accessable complexes consistent with # EXACTLY N distance restraints k.histogram(q, g['access_interspace'], g['subhists'], self.weights[n], d['nrestraints']) # Count the violations of each restraint for all complexes # consistent with EXACTLY N restraints k.count_violations(q, g['restraints'], self.rotations[n], g['access_interspace'], g['viol_counter'], self.weights[n]) # inform user if _stdout.isatty(): self._print_progress(n, g['nrot'], time0) # wait for calculations to finish self.queue.finish() # transfer the data from GPU to CPU # get the number of accessible complexes and reduce the subhistograms # to the final histogram access_complexes = g['subhists'].get().sum(axis=0) # account for the fact that we are counting the number of accessible # complexes consistent with EXACTLY N restraints access_complexes[0] = tot_complexes.get() - sum(access_complexes[1:]) d['accessible_complexes'] = access_complexes d['accessible_interaction_space'] = g['best_access_interspace'].get() # get the violation submatrices and reduce it to the final violation # matrix d['violations'] = g['viol_counter'].get().sum(axis=0)
def _cl_get_access_interspace(self): cl_array.maximum(self._cl_red_interspace, self._cl_access_interspace, self._cl_access_interspace) self.queue.finish()
def _gpu_search(self): """Method that actually performs the exhaustive search on the GPU""" # make shortcuts d = self.data g = self.gpu_data q = self.queue k = g['k'] # initalize the total number of sampled complexes tot_complexes = cl_array.sum(g['interspace'], dtype=np.float32) # initialize time time0 = _time() # loop over all rotations for n in xrange(g['nrot']): # rotate the scanning chain object k.rotate_image3d(q, g['sampler'], g['im_lsurf'], self.rotations[n], g['lsurf'], d['im_center']) # perform the FFTs and calculate the clashing and interaction volume k.rfftn(q, g['lsurf'], g['ft_lsurf']) k.c_conj_multiply(q, g['ft_lsurf'], g['ft_rcore'], g['ft_clashvol']) k.irfftn(q, g['ft_clashvol'], g['clashvol']) k.c_conj_multiply(q, g['ft_lsurf'], g['ft_rsurf'], g['ft_intervol']) k.irfftn(q, g['ft_intervol'], g['intervol']) # determine at every position if the conformation is a proper complex k.touch(q, g['clashvol'], g['max_clash'], g['intervol'], g['min_interaction'], g['interspace']) if self.distance_restraints: k.fill(q, g['restspace'], 0) # determine the space that is consistent with a number of # distance restraints k.distance_restraint(q, g['restraints'], self.rotations[n], g['restspace']) # get the accessible interaction space also consistent with a # certain number of distance restraints k.multiply(q, g['restspace'], g['interspace'], g['access_interspace']) # calculate the total number of complexes, while taking into # account orientational/rotational bias tot_complexes += cl_array.sum(g['interspace'], dtype=np.float32)*np.float32(self.weights[n]) # take at every position in space the maximum number of consistent # restraints for later visualization cl_array.maximum(g['best_access_interspace'], g['access_interspace'], g['best_access_interspace']) # calculate the number of accessable complexes consistent with # EXACTLY N distance restraints k.histogram(q, g['access_interspace'], g['subhists'], self.weights[n], d['nrestraints']) # Count the violations of each restraint for all complexes # consistent with EXACTLY N restraints k.count_violations(q, g['restraints'], self.rotations[n], g['access_interspace'], g['viol_counter'], self.weights[n]) # inform user if _stdout.isatty(): self._print_progress(n, g['nrot'], time0) # wait for calculations to finish self.queue.finish() # transfer the data from GPU to CPU # get the number of accessible complexes and reduce the subhistograms # to the final histogram access_complexes = g['subhists'].get().sum(axis=0) # account for the fact that we are counting the number of accessible # complexes consistent with EXACTLY N restraints access_complexes[0] = tot_complexes.get() - sum(access_complexes[1:]) d['accessible_complexes'] = access_complexes d['accessible_interaction_space'] = g['best_access_interspace'].get() # get the violation submatrices and reduce it to the final violation # matrix d['violations'] = g['viol_counter'].get().sum(axis=0)