src/luxrays/idevices/openclidevice.cpp
//------------------------------------------------------------------------------
// Memory allocation for GPU only applications
//------------------------------------------------------------------------------
Memory allocation for GPU only applications
Memory allocation for GPU only applications
void OpenCLIntersectionDevice::AllocBuffer(const cl_mem_flags clFlags, cl::Buffer **buff,
		void *src, const size_t size, const std::string &desc) {
	if (GetDeviceDesc()->GetMaxMemoryAllocSize() < size) {
		std::stringstream ss;
		ss << "The " << desc << " buffer is too big for " << GetName() <<
				" device (i.e. CL_DEVICE_MAX_MEM_ALLOC_SIZE=" <<
				GetDeviceDesc()->GetMaxMemoryAllocSize() <<
				"): try to reduce related parameters";
		throw std::runtime_error(ss.str());
	}
	if (!size) {
		if (*buff) {
			FreeMemory((*buff)->getInfo<CL_MEM_SIZE>());
			delete *buff;
		}
		*buff = NULL;
		return;
	}
	if (*buff) {
		if (size == (*buff)->getInfo<CL_MEM_SIZE>()) {
			if (src) {
				cl::CommandQueue &oclQueue = GetOpenCLQueue();
				oclQueue.enqueueWriteBuffer(**buff, CL_FALSE, 0, size, src);
			}
			return;
		} else {
			FreeBuffer(buff);
		}
	}
	cl::Context &oclContext = GetOpenCLContext();
	if (desc != "")
		LR_LOG(deviceContext, "[Device " << GetName() << "] " << desc <<
				" buffer size: " << ToMemString(size));
	*buff = new cl::Buffer(oclContext,
			clFlags,
			size, src);
	AllocMemory((*buff)->getInfo<CL_MEM_SIZE>());
}
void OpenCLIntersectionDevice :: AllocBuffer ( const cl_mem_flags clFlags , cl :: Buffer * * buff , void * src , const size_t size , const std :: string & desc ) { if ( GetDeviceDesc ( ) -> GetMaxMemoryAllocSize ( ) < size ) { std :: stringstream ss ; ss << " " << desc << " " << GetName ( ) << " " << GetDeviceDesc ( ) -> GetMaxMemoryAllocSize ( ) << " " ; throw std :: runtime_error ( ss . str ( ) ) ; } if ( ! size ) { if ( * buff ) { FreeMemory ( ( * buff ) -> getInfo < CL_MEM_SIZE > ( ) ) ; delete * buff ; } * buff = NULL ; return ; } if ( * buff ) { if ( size == ( * buff ) -> getInfo < CL_MEM_SIZE > ( ) ) { if ( src ) { cl :: CommandQueue & oclQueue = GetOpenCLQueue ( ) ; oclQueue . enqueueWriteBuffer ( * * buff , CL_FALSE , 0 , size , src ) ; } return ; } else { FreeBuffer ( buff ) ; } } cl :: Context & oclContext = GetOpenCLContext ( ) ; if ( desc != " " ) LR_LOG ( deviceContext , " " << GetName ( ) << " " << desc << " " << ToMemString ( size ) ) ; * buff = new cl :: Buffer ( oclContext , clFlags , size , src ) ; AllocMemory ( ( * buff ) -> getInfo < CL_MEM_SIZE > ( ) ) ; }
Memory allocation for GPU only applications
Memory allocation for GPU only applications
// Check if the buffer is too big // Handle the case of an empty buffer // Free the buffer // Check the size of the already allocated buffer // I can reuse the buffer; just update the content //LR_LOG(deviceContext, "[Device " << GetName() << "] " << desc << " buffer updated for size: " << (size / 1024) << "Kbytes"); // Free the buffer