bioem_cuda.cu 16 KB
Newer Older
Pilar Cossio's avatar
License  
Pilar Cossio committed
1 2 3 4
/* ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
        < BioEM software for Bayesian inference of Electron Microscopy images>
            Copyright (C) 2014 Pilar Cossio, David Rohr and Gerhard Hummer.
            Max Planck Institute of Biophysics, Frankfurt, Germany.
5

Pilar Cossio's avatar
License  
Pilar Cossio committed
6 7 8 9
                See license statement for terms of distribution.

   ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/

10 11 12 13 14 15 16 17 18 19 20 21
#define BIOEM_GPUCODE

#if defined(_WIN32)
#include <windows.h>
#endif

#include <iostream>
using namespace std;

#include "bioem_cuda_internal.h"
#include "bioem_algorithm.h"

22 23 24 25 26 27 28
#define checkCudaErrors(error) \
{ \
	if ((error) != cudaSuccess) \
	{ \
		printf("CUDA Error %d / %s (%s: %d)\n", error, cudaGetErrorString(error), __FILE__, __LINE__); \
		exit(1); \
	} \
29 30
}

David Rohr's avatar
David Rohr committed
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
static const char *cufftGetErrorStrung(cufftResult error)
{
    switch (error)
    {
        case CUFFT_SUCCESS:
            return "CUFFT_SUCCESS";

        case CUFFT_INVALID_PLAN:
            return "CUFFT_INVALID_PLAN";

        case CUFFT_ALLOC_FAILED:
            return "CUFFT_ALLOC_FAILED";

        case CUFFT_INVALID_TYPE:
            return "CUFFT_INVALID_TYPE";

        case CUFFT_INVALID_VALUE:
            return "CUFFT_INVALID_VALUE";

        case CUFFT_INTERNAL_ERROR:
            return "CUFFT_INTERNAL_ERROR";

        case CUFFT_EXEC_FAILED:
            return "CUFFT_EXEC_FAILED";

        case CUFFT_SETUP_FAILED:
            return "CUFFT_SETUP_FAILED";

        case CUFFT_INVALID_SIZE:
            return "CUFFT_INVALID_SIZE";

        case CUFFT_UNALIGNED_DATA:
            return "CUFFT_UNALIGNED_DATA";
    }
    return "UNKNOWN";
}

68 69 70 71
bioem_cuda::bioem_cuda()
{
	deviceInitialized = 0;
	GPUAlgo = getenv("GPUALGO") == NULL ? 2 : atoi(getenv("GPUALGO"));
72 73
	GPUAsync = getenv("GPUASYNC") == NULL ? 1 : atoi(getenv("GPUASYNC"));
	GPUWorkload = getenv("GPUWORKLOAD") == NULL ? 100 : atoi(getenv("GPUWORKLOAD"));
74
	GPUDualStream = getenv("GPUDUALSTREAM") == NULL ? 1 : atoi(getenv("GPUDUALSTREAM"));
75 76 77 78 79 80 81
}

bioem_cuda::~bioem_cuda()
{
	deviceExit();
}

82
__global__ void compareRefMap_kernel(const int iOrient, const int iConv, const myfloat_t* pMap, bioem_Probability pProb, const bioem_param_device param, const bioem_RefMap_Mod RefMap, const int cent_x, const int cent_y, const int maxRef)
83 84
{
	const int iRefMap = myBlockIdxX * myBlockDimX + myThreadIdxX;
85
	if (iRefMap < maxRef)
86
	{
87
		compareRefMap<0>(iRefMap, iOrient, iConv, pMap, pProb, param, RefMap, cent_x, cent_y);
88 89 90
	}
}

91
__global__ void compareRefMapShifted_kernel(const int iOrient, const int iConv, const myfloat_t* pMap, bioem_Probability pProb, const bioem_param_device param, const bioem_RefMap_Mod RefMap, const int maxRef)
92 93
{
	const int iRefMap = myBlockIdxX * myBlockDimX + myThreadIdxX;
94
	if (iRefMap < maxRef)
95
	{
96
		compareRefMapShifted<1>(iRefMap, iOrient, iConv, pMap, pProb, param, RefMap);
97 98 99
	}
}

100 101 102 103 104 105
__global__ void cudaZeroMem(void* ptr, size_t size)
{
	int* myptr = (int*) ptr;
	int mysize = size / sizeof(int);
	int myid = myBlockDimX * myBlockIdxX + myThreadIdxX;
	int mygrid = myBlockDimX * myGridDimX;
106
	for (int i = myid; i < mysize; i += mygrid) myptr[i] = 0;
107 108
}

109
__global__ void compareRefMapLoopShifts_kernel(const int iOrient, const int iConv, const myfloat_t* pMap, bioem_Probability pProb, const bioem_param_device param, const bioem_RefMap RefMap, const int blockoffset, const int nShifts, const int nShiftBits, const int maxRef)
110 111 112 113 114 115 116 117 118
{
	const size_t myid = (myBlockIdxX + blockoffset) * myBlockDimX + myThreadIdxX;
	const int iRefMap = myid >> (nShiftBits << 1);
	const int myRef = myThreadIdxX >> (nShiftBits << 1);
	const int myShiftIdx = (myid >> nShiftBits) & (nShifts - 1);
	const int myShiftIdy = myid & (nShifts - 1);
	const int myShift = myid & (nShifts * nShifts - 1);
	const int cent_x = myShiftIdx * param.GridSpaceCenter - param.maxDisplaceCenter;
	const int cent_y = myShiftIdy * param.GridSpaceCenter - param.maxDisplaceCenter;
119

120
	const bool threadActive = myShiftIdx < nShifts && myShiftIdy < nShifts && iRefMap < maxRef;
121

122
	compareRefMap<2>(iRefMap, iOrient, iConv, pMap, pProb, param, RefMap, cent_x, cent_y, myShift, nShifts * nShifts, myRef, threadActive);
123 124
}

125
__global__ void multComplexMap(const mycomplex_t* convmap, const mycomplex_t* refmap, mycuComplex_t* out, const int NumberPixelsTotal, const int MapSize, const int NumberMaps, const int Offset)
126 127
{
	if (myBlockIdxX >= NumberMaps) return;
128
	const mycuComplex_t* myin = (mycuComplex_t*) &refmap[(myBlockIdxX + Offset) * MapSize];
129
	const mycuComplex_t* myconv = (mycuComplex_t*) convmap;
130
	mycuComplex_t* myout = &out[myBlockIdxX * MapSize];
131
	for(int i = myThreadIdxX; i < NumberPixelsTotal; i += myBlockDimX)
132
	{
133 134 135 136 137 138 139
		mycuComplex_t val;
		const mycuComplex_t conv = myconv[i];
		const mycuComplex_t in = myin[i];

		val.x = conv.x * in.x + conv.y * in.y;
		val.y = conv.y * in.x - conv.x * in.y;
		myout[i] = val;
140 141 142
	}
}

143
__global__ void cuDoRefMapsFFT(const int iOrient, const int iConv, const myfloat_t* lCC, const myfloat_t sumC, const myfloat_t sumsquareC, bioem_Probability pProb, const bioem_param_device param, const bioem_RefMap RefMap, const int maxRef, const int Offset)
144
{
145
	if (myBlockIdxX * myBlockDimX + myThreadIdxX >= maxRef) return;
146 147
	const int iRefMap = myBlockIdxX * myBlockDimX + myThreadIdxX + Offset;
	const myfloat_t* mylCC = &lCC[(myBlockIdxX * myBlockDimX + myThreadIdxX) * param.NumberPixels * param.NumberPixels];
148
	doRefMapFFT(iRefMap, iOrient, iConv, mylCC, sumC, sumsquareC, pProb, param, RefMap);
149 150
}

151 152 153 154 155 156 157 158 159 160 161 162 163
template <class T> static inline T divup(T num, T divider) {return((num + divider - 1) / divider);}
static inline bool IsPowerOf2(int x) {return ((x > 0) && ((x & (x - 1)) == 0));}
#if defined(_WIN32)
static inline int ilog2 (int value)
{
	DWORD index;
	_BitScanReverse (&index, value);
	return(value);
}
#else
static inline int ilog2(int value) {return 31 - __builtin_clz(value);}
#endif

164
int bioem_cuda::compareRefMaps(int iOrient, int iConv, const myfloat_t* conv_map, mycomplex_t* localmultFFT, myfloat_t sumC, myfloat_t sumsquareC, const int startMap)
165
{
166 167 168 169 170
	if (startMap)
	{
		cout << "Error startMap not implemented for GPU Code\n";
		exit(1);
	}
171 172 173 174
	if (GPUAsync)
	{
		checkCudaErrors(cudaEventSynchronize(cudaEvent[iConv & 1]));
	}
175

176
	if (FFTAlgo)
177
	{
178
		memcpy(&pConvMapFFT_Host[(iConv & 1) * param.FFTMapSize], localmultFFT, param.FFTMapSize * sizeof(mycomplex_t));
179 180 181 182 183 184
		checkCudaErrors(cudaMemcpyAsync(&pConvMapFFT[(iConv & 1) * param.FFTMapSize], &pConvMapFFT_Host[(iConv & 1) * param.FFTMapSize], param.FFTMapSize * sizeof(mycomplex_t), cudaMemcpyHostToDevice, cudaStream[GPUAsync ? 2 : 0]));
		if (GPUAsync)
		{
			checkCudaErrors(cudaEventRecord(cudaEvent[2], cudaStream[2]));
			checkCudaErrors(cudaStreamWaitEvent(cudaStream[0], cudaEvent[2], 0));
		}
185
		if (GPUDualStream)
186
		{
187 188 189 190 191 192
			checkCudaErrors(cudaEventRecord(cudaFFTEvent[0], cudaStream[0]));
			checkCudaErrors(cudaStreamWaitEvent(cudaStream[1], cudaFFTEvent[0], 0));
		}
		for (int i = 0, j = 0; i < maxRef; i += CUDA_FFTS_AT_ONCE, j++)
		{
			if (!GPUDualStream) j = 0;
193
			const int num = min(CUDA_FFTS_AT_ONCE, maxRef - i);
194 195
			multComplexMap<<<num, CUDA_THREAD_COUNT, 0, cudaStream[j & 1]>>>(&pConvMapFFT[(iConv & 1) * param.FFTMapSize], pRefMapsFFT, pFFTtmp2[j & 1], param.param_device.NumberPixels * param.param_device.NumberFFTPixels1D, param.FFTMapSize, num, i);
			cufftResult err = mycufftExecC2R(i + CUDA_FFTS_AT_ONCE > maxRef ? plan[1][j & 1] : plan[0][j & 1], pFFTtmp2[j & 1], pFFTtmp[j & 1]);
David Rohr's avatar
David Rohr committed
196
			if (err != CUFFT_SUCCESS)
197
			{
David Rohr's avatar
David Rohr committed
198
				cout << "Error running CUFFT " << cufftGetErrorStrung(err) << "\n";
199 200
				exit(1);
			}
201
			cuDoRefMapsFFT<<<divup(num, CUDA_THREAD_COUNT), CUDA_THREAD_COUNT, 0, cudaStream[j & 1]>>>(iOrient, iConv, pFFTtmp[j & 1], sumC, sumsquareC, pProb_device, param.param_device, *gpumap, num, i);
202
		}
203
		checkCudaErrors(cudaGetLastError());
204 205 206 207 208
		if (GPUDualStream)
		{
			checkCudaErrors(cudaEventRecord(cudaFFTEvent[1], cudaStream[1]));
			checkCudaErrors(cudaStreamWaitEvent(cudaStream[0], cudaFFTEvent[1], 0));
		}
209 210 211
	}
	else
	{
212
		checkCudaErrors(cudaMemcpyAsync(pConvMap_device[iConv & 1], conv_map, sizeof(myfloat_t) * RefMap.refMapSize, cudaMemcpyHostToDevice, cudaStream[0]));
213 214

		if (GPUAlgo == 2) //Loop over shifts
215
		{
216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234
			const int nShifts = 2 * param.param_device.maxDisplaceCenter / param.param_device.GridSpaceCenter + 1;
			if (!IsPowerOf2(nShifts))
			{
				cout << "Invalid number of displacements, no power of two\n";
				exit(1);
			}
			if (CUDA_THREAD_COUNT % (nShifts * nShifts))
			{
				cout << "CUDA Thread count (" << CUDA_THREAD_COUNT << ") is no multiple of number of shifts (" << (nShifts * nShifts) << ")\n";
				exit(1);
			}
			if (nShifts > CUDA_MAX_SHIFT_REDUCE)
			{
				cout << "Too many displacements for CUDA reduction\n";
				exit(1);
			}
			const int nShiftBits = ilog2(nShifts);
			size_t totalBlocks = divup((size_t) maxRef * (size_t) nShifts * (size_t) nShifts, (size_t) CUDA_THREAD_COUNT);
			size_t nBlocks = CUDA_BLOCK_COUNT;
235
			for (size_t i = 0; i < totalBlocks; i += nBlocks)
236
			{
237
				compareRefMapLoopShifts_kernel<<<min(nBlocks, totalBlocks - i), CUDA_THREAD_COUNT, (CUDA_THREAD_COUNT * 2 + CUDA_THREAD_COUNT / (nShifts * nShifts) * 4) * sizeof(myfloat_t), cudaStream[0] >>> (iOrient, iConv, pConvMap_device[iConv & 1], pProb_device, param.param_device, *gpumap, i, nShifts, nShiftBits, maxRef);
238
			}
239
		}
240
		else if (GPUAlgo == 1) //Split shifts in multiple kernels
241
		{
242
			for (int cent_x = -param.param_device.maxDisplaceCenter; cent_x <= param.param_device.maxDisplaceCenter; cent_x = cent_x + param.param_device.GridSpaceCenter)
243
			{
244
				for (int cent_y = -param.param_device.maxDisplaceCenter; cent_y <= param.param_device.maxDisplaceCenter; cent_y = cent_y + param.param_device.GridSpaceCenter)
245
				{
246
					compareRefMap_kernel<<<divup(maxRef, CUDA_THREAD_COUNT), CUDA_THREAD_COUNT, 0, cudaStream[0]>>> (iOrient, iConv, pConvMap_device[iConv & 1], pProb_device, param.param_device, *pRefMap_device_Mod, cent_x, cent_y, maxRef);
247 248
				}
			}
249
		}
250
		else if (GPUAlgo == 0) //All shifts in one kernel
251
		{
252
			compareRefMapShifted_kernel<<<divup(maxRef, CUDA_THREAD_COUNT), CUDA_THREAD_COUNT, 0, cudaStream[0]>>> (iOrient, iConv, pConvMap_device[iConv & 1], pProb_device, param.param_device, *pRefMap_device_Mod, maxRef);
253
		}
254
		else
255
		{
256 257
			cout << "Invalid GPU Algorithm selected\n";
			exit(1);
258
		}
259
	}
260 261
	if (GPUWorkload < 100)
	{
262
		bioem::compareRefMaps(iOrient, iConv, conv_map, localmultFFT, sumC, sumsquareC, maxRef);
263
	}
264 265
	if (GPUAsync)
	{
266
		checkCudaErrors(cudaEventRecord(cudaEvent[iConv & 1], cudaStream[0]));
267
	}
268 269
	else
	{
270
		checkCudaErrors(cudaStreamSynchronize(cudaStream[0]));
271 272 273 274 275 276 277
	}
	return(0);
}

int bioem_cuda::deviceInit()
{
	deviceExit();
278

279 280
	if (FFTAlgo) GPUAlgo = 2;

281 282 283 284 285
	gpumap = new bioem_RefMap;
	memcpy(gpumap, &RefMap, sizeof(bioem_RefMap));
	if (FFTAlgo == 0)
	{
		checkCudaErrors(cudaMalloc(&maps, sizeof(myfloat_t) * RefMap.ntotRefMap * RefMap.refMapSize));
286 287 288 289 290 291 292 293 294 295 296 297 298

		if (GPUAlgo == 0 || GPUAlgo == 1)
		{
			pRefMap_device_Mod = (bioem_RefMap_Mod*) gpumap;
			bioem_RefMap_Mod* RefMapGPU = new bioem_RefMap_Mod;
			RefMapGPU->init(RefMap);
			checkCudaErrors(cudaMemcpy(maps, RefMapGPU->maps, sizeof(myfloat_t) * RefMap.ntotRefMap * RefMap.refMapSize, cudaMemcpyHostToDevice));
			delete RefMapGPU;
		}
		else
		{
			checkCudaErrors(cudaMemcpy(maps, RefMap.maps, sizeof(myfloat_t) * RefMap.ntotRefMap * RefMap.refMapSize, cudaMemcpyHostToDevice));
		}
299 300 301 302 303 304 305 306 307
	}
	checkCudaErrors(cudaMalloc(&sum, sizeof(myfloat_t) * RefMap.ntotRefMap));
	checkCudaErrors(cudaMemcpy(sum, RefMap.sum_RefMap, sizeof(myfloat_t) * RefMap.ntotRefMap, cudaMemcpyHostToDevice));
	checkCudaErrors(cudaMalloc(&sumsquare, sizeof(myfloat_t) * RefMap.ntotRefMap));
	checkCudaErrors(cudaMemcpy(sumsquare, RefMap.sumsquare_RefMap, sizeof(myfloat_t) * RefMap.ntotRefMap, cudaMemcpyHostToDevice));
	gpumap->maps = maps;
	gpumap->sum_RefMap = sum;
	gpumap->sumsquare_RefMap = sumsquare;

David Rohr's avatar
David Rohr committed
308
	checkCudaErrors(cudaMalloc(&pProb_memory, pProb_device.get_size(RefMap.ntotRefMap, param.nTotGridAngles)));
309
	for (int i = 0; i < 2; i++)
310
	{
311
		checkCudaErrors(cudaStreamCreate(&cudaStream[i]));
312
		checkCudaErrors(cudaEventCreate(&cudaEvent[i]));
313
		checkCudaErrors(cudaEventCreate(&cudaFFTEvent[i]));
314
		checkCudaErrors(cudaMalloc(&pConvMap_device[i], sizeof(myfloat_t) * RefMap.refMapSize));
315
	}
316 317 318 319 320
	if (GPUAsync)
	{
		checkCudaErrors(cudaStreamCreate(&cudaStream[2]));
		checkCudaErrors(cudaEventCreate(&cudaEvent[2]));
	}
321

322 323
	if (FFTAlgo)
	{
324
		checkCudaErrors(cudaMalloc(&pRefMapsFFT, RefMap.ntotRefMap * param.FFTMapSize * sizeof(mycomplex_t)));
325 326 327 328
		checkCudaErrors(cudaMalloc(&pFFTtmp2[0], CUDA_FFTS_AT_ONCE * param.FFTMapSize * 2 * sizeof(mycomplex_t)));
		checkCudaErrors(cudaMalloc(&pFFTtmp[0], CUDA_FFTS_AT_ONCE * param.param_device.NumberPixels * param.param_device.NumberPixels * 2 * sizeof(myfloat_t)));
		pFFTtmp2[1] = pFFTtmp2[0] + CUDA_FFTS_AT_ONCE * param.FFTMapSize;
		pFFTtmp[1] = pFFTtmp[0] + CUDA_FFTS_AT_ONCE * param.param_device.NumberPixels * param.param_device.NumberPixels;
329
		checkCudaErrors(cudaMalloc(&pConvMapFFT, param.FFTMapSize * sizeof(mycomplex_t) * 2));
330
		checkCudaErrors(cudaHostAlloc(&pConvMapFFT_Host, param.FFTMapSize * sizeof(mycomplex_t) * 2, 0));
331
		checkCudaErrors(cudaMemcpy(pRefMapsFFT, RefMap.RefMapsFFT, RefMap.ntotRefMap * param.FFTMapSize * sizeof(mycomplex_t), cudaMemcpyHostToDevice));
332 333
	}

334 335 336 337 338 339 340
	deviceInitialized = 1;
	return(0);
}

int bioem_cuda::deviceExit()
{
	if (deviceInitialized == 0) return(0);
341

342

David Rohr's avatar
David Rohr committed
343
	cudaFree(pProb_memory);
344 345
	cudaFree(sum);
	cudaFree(sumsquare);
346
	for (int i = 0; i < 2; i++)
347
	{
348
		cudaStreamDestroy(cudaStream[i]);
349
		cudaEventDestroy(cudaEvent[i]);
350
		cudaEventDestroy(cudaFFTEvent[i]);
351
		cudaFree(pConvMap_device[i]);
352
	}
353 354 355 356
	if (FFTAlgo)
	{
		cudaFree(pRefMapsFFT);
		cudaFree(pConvMapFFT);
357
		cudaFreeHost(pConvMapFFT_Host);
358 359
		cudaFree(pFFTtmp[0]);
		cudaFree(pFFTtmp2[0]);
360
	}
361 362 363 364 365 366 367 368
	else
	{
		cudaFree(maps);
	}
	if (GPUAlgo == 0 || GPUAlgo == 1)
	{
		cudaFree(pRefMap_device_Mod);
	}
369 370 371 372 373 374
	if (GPUAsync)
	{
		cudaStreamDestroy(cudaStream[2]);
		cudaEventDestroy(cudaEvent[2]);
	}

375
	delete gpumap;
376
	cudaThreadExit();
377

378 379 380 381 382 383
	deviceInitialized = 0;
	return(0);
}

int bioem_cuda::deviceStartRun()
{
David Rohr's avatar
David Rohr committed
384 385 386 387 388 389 390 391 392 393 394 395
	if (GPUWorkload >= 100)
	{
		maxRef = RefMap.ntotRefMap;
		pProb_host = &pProb;
	}
	else
	{
		maxRef = (size_t) RefMap.ntotRefMap * (size_t) GPUWorkload / 100;
		pProb_host = new bioem_Probability;
		pProb_host->init(maxRef, param.nTotGridAngles, *this);
		pProb_host->copyFrom(&pProb, *this);
	}
396

David Rohr's avatar
David Rohr committed
397 398 399 400
	pProb_device = *pProb_host;
	pProb_device.ptr = pProb_memory;
	pProb_device.set_pointers();
	checkCudaErrors(cudaMemcpyAsync(pProb_device.ptr, pProb_host->ptr, pProb_host->get_size(maxRef, param.nTotGridAngles), cudaMemcpyHostToDevice, cudaStream[0]));
401 402 403

	if (FFTAlgo)
	{
404
		for (int j = 0;j < 2;j++)
405
		{
406
			for (int i = 0; i < 2; i++)
407
			{
408
				if (i && maxRef % CUDA_FFTS_AT_ONCE == 0) continue;
409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424
				int n[2] = {param.param_device.NumberPixels, param.param_device.NumberPixels};
				if (cufftPlanMany(&plan[i][j], 2, n, NULL, 1, param.FFTMapSize, NULL, 1, 0, MY_CUFFT_C2R, i ? (maxRef % CUDA_FFTS_AT_ONCE) : CUDA_FFTS_AT_ONCE) != CUFFT_SUCCESS)
				{
					cout << "Error planning CUFFT\n";
					exit(1);
				}
				if (cufftSetCompatibilityMode(plan[i][j], CUFFT_COMPATIBILITY_NATIVE) != CUFFT_SUCCESS)
				{
					cout << "Error planning CUFFT compatibility\n";
					exit(1);
				}
				if (cufftSetStream(plan[i][j], cudaStream[j]) != CUFFT_SUCCESS)
				{
					cout << "Error setting CUFFT stream\n";
					exit(1);
				}
425
			}
426
			if (!GPUDualStream) break;
427 428
		}
	}
429 430 431 432 433
	return(0);
}

int bioem_cuda::deviceFinishRun()
{
434
	if (GPUAsync) cudaStreamSynchronize(cudaStream[0]);
David Rohr's avatar
David Rohr committed
435
	checkCudaErrors(cudaMemcpyAsync(pProb_host->ptr, pProb_device.ptr, pProb_host->get_size(maxRef, param.nTotGridAngles), cudaMemcpyDeviceToHost, cudaStream[0]));
436

437 438
	if (FFTAlgo)
	{
439 440
		for (int j = 0;j < 2;j++)
		{
441 442 443 444 445
			for (int i = 0; i < 2; i++)
			{
				if (i && maxRef % CUDA_FFTS_AT_ONCE == 0) continue;
				cufftDestroy(plan[i][j]);
			}
446 447
			if (!GPUDualStream) break;
		}
448
	}
David Rohr's avatar
David Rohr committed
449 450 451 452
	cudaThreadSynchronize();
	if (GPUWorkload < 100)
	{
		pProb.copyFrom(pProb_host, *this);
453
		free_device_host(pProb_host->ptr);
David Rohr's avatar
David Rohr committed
454 455
		delete[] pProb_host;
	}
456

457 458 459
	return(0);
}

460 461 462 463 464 465 466 467 468 469 470 471
void* bioem_cuda::malloc_device_host(size_t size)
{
	void* ptr;
	checkCudaErrors(cudaHostAlloc(&ptr, size, 0));
	return(ptr);
}

void bioem_cuda::free_device_host(void* ptr)
{
	cudaFreeHost(ptr);
}

472 473 474 475
bioem* bioem_cuda_create()
{
	return new bioem_cuda;
}