cudaHandleError.h 1.12 KB
#include <stdio.h>
#include "cuda_runtime.h"
#include "device_launch_parameters.h"

#ifndef CUDA_HANDLE_ERROR_H
#define CUDA_HANDLE_ERROR_H

//handle error macro
static void HandleError( cudaError_t err, const char *file,  int line ) {
   	if (err != cudaSuccess) {
			FILE* outfile = fopen("cudaErrorLog.txt", "w");
      		fprintf(outfile,  "%s in %s at line %d\n", cudaGetErrorString( err ),  file, line );
			fclose(outfile);
       		exit( EXIT_FAILURE );
			printf("%s in %s at line %d\n", cudaGetErrorString( err ),  file, line );
   	}
}
#define HANDLE_ERROR( err ) (HandleError( err, __FILE__, __LINE__ ))

static cudaEvent_t tStartEvent;
static cudaEvent_t tStopEvent;
static void gpuStartTimer()
{
	//set up timing events
	cudaEventCreate(&tStartEvent);
	cudaEventCreate(&tStopEvent);
	cudaEventRecord(tStartEvent, 0);
}

static float gpuStopTimer()
{
	cudaEventRecord(tStopEvent, 0);
	cudaEventSynchronize(tStopEvent);
	float elapsedTime;
	cudaEventElapsedTime(&elapsedTime, tStartEvent, tStopEvent);
	cudaEventDestroy(tStartEvent);
	cudaEventDestroy(tStopEvent);
	return elapsedTime;
}

#endif