 		}
 	}
 	return answer;
+}
+
+void checkCUDAError(const char *msg)
+{
+	cudaError_t err = cudaGetLastError();
+	if(cudaSuccess != err)  {
+		fprintf(stderr, "Cuda error: %s: %s.\n", msg, cudaGetErrorString( err) );
+		exit(EXIT_FAILURE);
+	}
 }
 
 static char nearestNeighbor(struct OCRkit *ocrKit, double *questionWeights)

		totalScore = numerator / (sqrt(denominatorA) * sqrt(denominatorB));
	}

	__syncthreads(); 

	// save cosine similarity score
	g_scores[idx] = totalScore;
}

	// run the kernel
	nearestNeighborGPUa<<< dimGrid,dimBlock,sharedMemSize >>>(klimit, dimensionality, d_charWeights, d_qWeights, d_scores);
	cudaThreadSynchronize();
	checkCUDAError("kernel");

	// get the scores
	CUDA_SAFE_CALL(cudaMemcpy(h_scores, d_scores, scoreMemSize, cudaMemcpyDeviceToHost));
	checkCUDAError("memcpy");

	char answer = '?';
	int maxScore = -999;
