def makeStructures(self): print("Making Structures") mf = cl.mem_flags ss = StructSizes() ss = ss.get_struct_sizes() # MAKING STRUCTURING ELEMENT self.strEl = VglStrEl() self.strEl.constructorFromTypeNdim(vc.VGL_STREL_GAUSS(), 5) # gaussian blur of size 5 self.vglClStrEl = self.strEl.asVglClStrEl() vgl_strel_obj = np.zeros(ss[0], np.uint8) vgl_shape_obj = np.zeros(ss[6], np.uint8) # COPYING DATA AS BYTES TO HOST BUFFER self.copy_into_byte_array(self.vglClStrEl.data, vgl_strel_obj, ss[1]) self.copy_into_byte_array(self.vglClStrEl.ndim, vgl_strel_obj, ss[2]) self.copy_into_byte_array(self.vglClStrEl.shape, vgl_strel_obj, ss[3]) self.copy_into_byte_array(self.vglClStrEl.offset, vgl_strel_obj, ss[4]) self.copy_into_byte_array(self.vglClStrEl.size, vgl_strel_obj, ss[5]) image_cl_shape = self.vglimage.getVglShape().asVglClShape() self.copy_into_byte_array(image_cl_shape.ndim, vgl_shape_obj, ss[7]) self.copy_into_byte_array(image_cl_shape.shape, vgl_shape_obj, ss[8]) self.copy_into_byte_array(image_cl_shape.offset, vgl_shape_obj, ss[9]) self.copy_into_byte_array(image_cl_shape.size, vgl_shape_obj, ss[10]) # CREATING DEVICE BUFFER TO HOLD STRUCT DATA self.window = cl.Buffer(self.ctx, mf.READ_ONLY, vgl_strel_obj.nbytes) self.img_shape = cl.Buffer(self.ctx, mf.READ_ONLY, vgl_shape_obj.nbytes) # COPYING DATA FROM HOST TO DEVICE cl.enqueue_copy(self.queue, self.window, vgl_strel_obj, is_blocking=True) cl.enqueue_copy(self.queue, self.img_shape, vgl_shape_obj, is_blocking=True)
def loadCL(self, filepath): print("Loading OpenCL Kernel") self.kernel_file = open(filepath, "r") buildDir = self.getDir(filepath) self.build_options = "" self.build_options = self.build_options + "-I "+buildDir self.build_options = self.build_options + " -D VGL_SHAPE_NCHANNELS={0}".format(vc.VGL_SHAPE_NCHANNELS()) self.build_options = self.build_options + " -D VGL_SHAPE_WIDTH={0}".format(vc.VGL_SHAPE_WIDTH()) self.build_options = self.build_options + " -D VGL_SHAPE_HEIGHT={0}".format(vc.VGL_SHAPE_HEIGHT()) self.build_options = self.build_options + " -D VGL_SHAPE_LENGTH={0}".format(vc.VGL_SHAPE_LENGTH()) self.build_options = self.build_options + " -D VGL_MAX_DIM={0}".format(vc.VGL_MAX_DIM()) self.build_options = self.build_options + " -D VGL_ARR_SHAPE_SIZE={0}".format(vc.VGL_ARR_SHAPE_SIZE()) self.build_options = self.build_options + " -D VGL_ARR_CLSTREL_SIZE={0}".format(vc.VGL_ARR_CLSTREL_SIZE()) self.build_options = self.build_options + " -D VGL_STREL_CUBE={0}".format(vc.VGL_STREL_CUBE()) self.build_options = self.build_options + " -D VGL_STREL_CROSS={0}".format(vc.VGL_STREL_CROSS()) self.build_options = self.build_options + " -D VGL_STREL_GAUSS={0}".format(vc.VGL_STREL_GAUSS()) self.build_options = self.build_options + " -D VGL_STREL_MEAN={0}".format(vc.VGL_STREL_MEAN()) #print("Build Options:\n", self.build_options) # READING THE HEADER FILES BEFORE COMPILING THE KERNEL for file in glob.glob(buildDir+"/*.h"): self.pgr = cl.Program(self.ctx, open(file, "r")) if ((self.builded == False)): self.pgr = cl.Program(self.ctx, self.kernel_file.read()) self.pgr.build(options=self.build_options) #self.pgr.build() self.builded = True else: print("Kernel already builded. Going to next step...") self.kernel_file.close()
def loadVglObjects(self): self.vglShape = VglShape() self.vglShape.constructor2DShape(self.img_nchannels, self.img_shape[1], self.img_shape[0]) self.vglClShape = self.vglShape.asVglClShape() self.strEl = VglStrEl() self.strEl.constructorFromTypeNdim(vc.VGL_STREL_GAUSS(), 5) # gaussian blur of size 5 self.vglClStrEl = self.strEl.asVglClStrEl() self.makeStructures()
def constructorFromTypeNdim(self, Type, ndim): shape = np.zeros(vc.VGL_MAX_DIM(), np.int32) shape[0] = 1 shape[2] = 1 for i in range(1, ndim + 1): # ndim+1 cuz in c++ is for i <= ndim shape[i] = 3 vglShape = VglShape() vglShape.constructorFromShapeNdimBps(shape, ndim) size = vglShape.getSize() data = np.zeros((size), np.float32) index = 0 if (Type == vc.VGL_STREL_CROSS()): coord = np.zeros((vc.VGL_ARR_SHAPE_SIZE()), np.int32) for i in range(0, size): data[i] = np.float32(0.0) for d in range(1, ndim + 1): coord[d] = np.int32(1) index = vglShape.getIndexFromCoord(coord) data[index] = np.float32(1.0) for d in range(1, ndim + 1): coord[d] = np.int32(0) index = vglShape.getIndexFromCoord(coord) data[index] = np.float32(1.0) coord[d] = np.int32(2) index = vglShape.getIndexFromCoord(coord) data[index] = np.float32(1.0) coord[d] = 1 elif (Type == vc.VGL_STREL_GAUSS()): coord = np.zeros((vc.VGL_ARR_SHAPE_SIZE()), np.int32) coord[0] = np.int32(0) size = vglShape.getSize() for i in range(0, size): val = np.float32(1.0) vglShape.getCoordFromIndex(i, coord) for d in range(1, ndim + 1): if (coord[d] == 1): val = val * np.float32(0.5) else: val = val * np.float32(0.25) data[i] = val elif (Type == vc.VGL_STREL_MEAN()): for i in range(0, size): data[i] = 1.0 / size elif (Type == vc.VGL_STREL_CUBE()): for i in range(0, size): data[i] = 1.0 else: for i in range(0, size): data[i] = 1.0 self.constructorFromDataVglShape(data, vglShape)