cudautils.h
Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2011, Mårten Björkman (celle@csc.kth.se) 
00003  * All rights reserved.
00004  *
00005  * Redistribution and use in source and binary forms, with or without
00006  * modification, are permitted provided that the following conditions are
00007  * met:
00008  *
00009  *  1.Redistributions of source code must retain the above copyright
00010  *    notice, this list of conditions and the following disclaimer.
00011  *  2.Redistributions in binary form must reproduce the above
00012  *    copyright notice, this list of conditions and the following
00013  *    disclaimer in the documentation and/or other materials provided
00014  *    with the distribution.  
00015  *  3.The name of Mårten Björkman may not be used to endorse or
00016  *    promote products derived from this software without specific
00017  *    prior written permission.
00018  *
00019  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00020  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00021  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
00022  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
00023  * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
00024  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
00025  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
00026  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
00027  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00028  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00029  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00030  */
00031 
00032 #ifndef CUDAUTILS_H
00033 #define CUDAUTILS_H
00034 
00037 #include <cstdio>
00038 #include <iostream>
00039 
00040 #define safeCall(err)       __safeCall(err, __FILE__, __LINE__)
00041 #define safeThreadSync()    __safeThreadSync(__FILE__, __LINE__)
00042 #define checkMsg(msg)       __checkMsg(msg, __FILE__, __LINE__)
00043 
00045 
00050 inline void __safeCall(cudaError err, const char *file, const int line)
00051 {
00052   if (cudaSuccess != err) {
00053     fprintf(stderr, "safeCall() Runtime API error in file <%s>, line %i : %s.\n", file, line, cudaGetErrorString(err));
00054     exit(-1);
00055   }
00056 }
00057 
00059 
00063 inline void __safeThreadSync(const char *file, const int line)
00064 {
00065   cudaError err = cudaThreadSynchronize();
00066   if (cudaSuccess != err) {
00067     fprintf(stderr, "threadSynchronize() Driver API error in file '%s' in line %i : %s.\n", file, line, cudaGetErrorString(err));
00068     exit(-1);
00069   }
00070 }
00071 
00073 
00078 inline void __checkMsg(const char *errorMessage, const char *file, const int line)
00079 {
00080   cudaError_t err = cudaGetLastError();
00081   if (cudaSuccess != err) {
00082     fprintf(stderr, "checkMsg() CUDA error: %s in file <%s>, line %i : %s.\n", errorMessage, file, line, cudaGetErrorString(err));
00083     exit(-1);
00084   }
00085 }
00086 
00088 inline bool deviceInit(int dev)
00089 {
00090   int deviceCount;
00091   safeCall(cudaGetDeviceCount(&deviceCount));
00092   if (deviceCount == 0) {
00093     fprintf(stderr, "CUDA error: no devices supporting CUDA.\n");
00094     return false;
00095   }
00096   if (dev < 0) dev = 0;                                         
00097   if (dev > deviceCount-1) dev = deviceCount - 1;
00098   cudaDeviceProp deviceProp;
00099   safeCall(cudaGetDeviceProperties(&deviceProp, dev));
00100   if (deviceProp.major < 1) {
00101     fprintf(stderr, "error: device does not support CUDA.\n");
00102     return false;                                       
00103   }
00104   safeCall(cudaSetDevice(dev));
00105   return true;
00106 }
00107 
00108 
00110 class TimerGPU {
00111 public:
00113   cudaEvent_t start;
00115   cudaEvent_t stop;
00117   cudaStream_t stream;
00119 
00122   TimerGPU(cudaStream_t stream_ = 0) : stream(stream_) {
00123     cudaEventCreate(&start); 
00124     cudaEventCreate(&stop); 
00125     cudaEventRecord(start, stream); 
00126   }
00127   ~TimerGPU() {
00128     cudaEventDestroy(start); 
00129     cudaEventDestroy(stop);  
00130   }
00132 
00134   float read() {
00135     cudaEventRecord(stop, stream); 
00136     cudaEventSynchronize(stop); 
00137     float time;
00138     cudaEventElapsedTime(&time, start, stop);
00139     return time;
00140   }
00141 };
00142 
00143 #endif
00144 


active_realtime_segmentation
Author(s): Mårten Björkman. Maintained by Jeannette Bohg
autogenerated on Fri Jan 3 2014 12:02:50