inline svm_ptr<T> svm_alloc(const context &context, size_t size, cl_svm_mem_flags flags = CL_MEM_READ_WRITE, unsigned int alignment = 0) { svm_ptr<T> ptr(clSVMAlloc(context.get(), flags, size * sizeof(T), alignment)); if(!ptr.get()){ BOOST_THROW_EXCEPTION(opencl_error(CL_MEM_OBJECT_ALLOCATION_FAILURE)); } return ptr; }
error* svm_alloc( clobj_t _ctx, cl_mem_flags flags, size_t size, cl_uint alignment, void **result) { #if PYOPENCL_CL_VERSION >= 0x2000 auto ctx = static_cast<context*>(_ctx); return c_handle_retry_mem_error([&] { *result = clSVMAlloc(ctx->data(), flags, size, alignment); if (!*result) throw clerror("clSVMalloc", CL_INVALID_VALUE, "(allocation failure, unspecified reason)"); }); #else PYOPENCL_UNSUPPORTED_BEFORE(clSVMAlloc, "CL 2.0") #endif }
clMemRAII( const cl_command_queue cl_queue, void* cl_malloc, const size_t cl_size = 0, const cl_svm_mem_flags cl_flags = CL_MEM_READ_WRITE): clMem( nullptr ), clOwner(false) { clQueue = cl_queue; clMem = static_cast< pType* >( cl_malloc ); if(cl_size > 0) { cl_context ctx = NULL; ::clGetCommandQueueInfo(clQueue, CL_QUEUE_CONTEXT, sizeof( cl_context ), &ctx, NULL); cl_int status = 0; clMem = static_cast< pType* > (clSVMAlloc(ctx, cl_flags, cl_size * sizeof(pType), 0)); clOwner = true; } ::clRetainCommandQueue( clQueue ); }
JNIEXPORT jlong JNICALL Java_org_lwjgl_opencl_CL20_nclSVMAlloc(JNIEnv *__env, jclass clazz, jlong contextAddress, jlong flags, jlong size, jint alignment, jlong __functionAddress) { cl_context context = (cl_context)(intptr_t)contextAddress; clSVMAllocPROC clSVMAlloc = (clSVMAllocPROC)(intptr_t)__functionAddress; UNUSED_PARAMS(__env, clazz) return (jlong)(intptr_t)clSVMAlloc(context, flags, (size_t)size, alignment); }
/// Allocates SVM vector on the given device. svm_vector(const cl::CommandQueue &q, size_t n) : n(n), q(q), p(NULL) { p = static_cast<T*>(clSVMAlloc(backend::get_context_id(q), CL_MEM_READ_WRITE, n * sizeof(T), 0)); }
UINT CFrontEnd::uiBuildOCLKernels() { //allocate the required memory size_t uiReqQueueSize = OCL_REQ_QUEUE_SIZE*sizeof(TQueuedRequest); size_t uiNodePoolSize = OCL_NODE_POOL_SIZE*sizeof(TLLNode); size_t uiHashTableSize = OCL_HASH_TABLE_SIZE*sizeof(TLLNode); size_t uiMiscDataSize = sizeof(TMiscData); size_t uiSVMSize = uiReqQueueSize + uiNodePoolSize + uiHashTableSize + uiMiscDataSize; cl_svm_mem_flags tSVMFlags = CL_MEM_READ_WRITE | CL_MEM_SVM_FINE_GRAIN_BUFFER; //CL_MEM_SVM_ATOMICS; pSVMBuf = (void *)clSVMAlloc(pOclContext->oclContext, tSVMFlags, uiSVMSize, 0); if(pSVMBuf == NULL) { std::cout << "SVMAlloc failed." << std::endl; return HTS_NOT_OK; } //allocate different svm pointers pOclReqQueue = (TQueuedRequest *)pSVMBuf; pHashTable = (TLLNode*)((char *)pOclReqQueue + uiReqQueueSize); pNodePool = (TLLNode*)((char *)pHashTable + uiHashTableSize); pMiscData = (TMiscData*)((char *)pNodePool + uiNodePoolSize); //make node pool and hash table to be same pNodePool = pHashTable; //initialize svm buffers -- Hash table for(UINT i = 0; i < OCL_HASH_TABLE_SIZE; ++i) { for(UINT j = 0; j < OCL_WG_SIZE; j++) pNodePool[i].pE[j] = 0 ; pNodePool[i].uiNext = 0; } pMiscData->uiHeadIndex = OCL_HASH_TABLE_SIZE; cl_uint uiStartIndex = OCL_HASH_TABLE_SIZE; cl_uint uiEndIndex = OCL_HASH_TABLE_SIZE + OCL_NODE_POOL_SIZE -1; for(cl_uint i = uiStartIndex; i < uiEndIndex; ++i) { /* set the pointer to next and also set free bit */ cl_uint j = SET_PTR(i+1); j = SET_FBIT(j); pNodePool[i].uiNext = j; } pNodePool[uiEndIndex].uiNext = 0; //create the program std::string sourceStr; cl_uint iStatus = (cl_int)(uiConvertToString(FILENAME, sourceStr)); const char *source = sourceStr.c_str(); size_t sourceSize[] = {strlen(source)}; oclProgram = clCreateProgramWithSource(pOclContext->oclContext, 1, &source, sourceSize, NULL); std::cout << "building program" << std::endl; const char* options = "-I. -cl-std=CL2.0"; iStatus = clBuildProgram(oclProgram, 1, pOclContext->pOclDevices, options, NULL, NULL); if(iStatus != CL_SUCCESS) { //get the build error size_t uiBuildLogSize; clGetProgramBuildInfo(oclProgram, pOclContext->pOclDevices[0], CL_PROGRAM_BUILD_LOG, 0, NULL, &uiBuildLogSize); char *pBuildLog = (char *)malloc(uiBuildLogSize*sizeof(char)); clGetProgramBuildInfo(oclProgram, pOclContext->pOclDevices[0], CL_PROGRAM_BUILD_LOG, uiBuildLogSize, (void *)pBuildLog, NULL); std::cout << "failed to build program." << std::endl; std::cout << "BUILD LOG---" << std::endl; std::cout << pBuildLog << std::endl; std::cout << "------------" << std::endl; return HTS_NOT_OK; } /* create kernels */ std::cout << "creating kernels" << std::endl; oclKernel = clCreateKernel(oclProgram, "HTSTopKernel", NULL); return HTS_OK; }