00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #include "msmpot_internal.h"
00019
00020 #undef NELEMS
00021 #define NELEMS(a) ((int)(sizeof(a)/sizeof(a[0])))
00022
00023
00024
00025
00026 static const char *ERROR_STRING[] = {
00027 "success",
00028 "assertion failed",
00029 "memory allocation error",
00030 "illegal parameter",
00031 "unsupported request",
00032 "CUDA device request failed",
00033 "CUDA memory allocation error",
00034 "CUDA memory copy error",
00035 "CUDA kernel execution failed",
00036 "CUDA kernel does not support request",
00037 "unknown error",
00038 };
00039
00040
00041 const char *Msmpot_error_string(int retcode) {
00042 if (retcode < 0 || retcode >= NELEMS(ERROR_STRING)) {
00043 retcode = NELEMS(ERROR_STRING) - 1;
00044 }
00045 return ERROR_STRING[retcode];
00046 }
00047
00048
00049 #ifdef MSMPOT_DEBUG
00050
00051
00052
00053
00054 int Msmpot_report_error(int err, const char *msg, const char *fn, int ln) {
00055 if (msg) {
00056 fprintf(stderr, "MSMPOT ERROR (%s,%d): %s: %s\n",
00057 fn, ln, Msmpot_error_string(err), msg);
00058 }
00059 else {
00060 fprintf(stderr, "MSMPOT ERROR (%s,%d): %s\n",
00061 fn, ln, Msmpot_error_string(err));
00062 }
00063 return err;
00064 }
00065
00066 #endif
00067
00068
00069 Msmpot *Msmpot_create(void) {
00070 Msmpot *msm = (Msmpot *) calloc(1, sizeof(Msmpot));
00071 if (NULL == msm) return NULL;
00072 #ifdef MSMPOT_CUDA
00073 msm->msmcuda = Msmpot_cuda_create();
00074 if (NULL == msm->msmcuda) {
00075 Msmpot_destroy(msm);
00076 return NULL;
00077 }
00078 #endif
00079 Msmpot_set_defaults(msm);
00080 return msm;
00081 }
00082
00083
00084 void Msmpot_destroy(Msmpot *msm) {
00085 #ifdef MSMPOT_CUDA
00086 if (msm->msmcuda) Msmpot_cuda_destroy(msm->msmcuda);
00087 #endif
00088 Msmpot_cleanup(msm);
00089 free(msm);
00090 }
00091
00092
00093 int Msmpot_use_cuda(Msmpot *msm, const int *devlist, int listlen,
00094 int cuda_optional) {
00095 if (NULL == devlist || listlen <= 0) {
00096 return ERROR(MSMPOT_ERROR_PARAM);
00097 }
00098 #ifdef MSMPOT_CUDA
00099 msm->devlist = devlist;
00100 msm->devlistlen = listlen;
00101 msm->cuda_optional = cuda_optional;
00102 msm->use_cuda = 1;
00103 return MSMPOT_SUCCESS;
00104 #else
00105 return ERRMSG(MSMPOT_ERROR_SUPPORT,
00106 "CUDA support is not available in this build");
00107 #endif
00108 }