void mexFunction( int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[] ) { // Garbage collector MyGC mygc = MyGC(); if (nrhs!=2) mexErrMsgTxt("Wrong number of arguments"); // tmp mxArray *lhs[2]; mexCallMATLAB(1, &lhs[0], 0, &lhs[0], "GPUmanager");\ GPUmanager * GPUman = (GPUmanager *) (UINTPTR mxGetScalar(lhs[0])); mxDestroyArray(lhs[0]); if (GPUman->getCompileMode()==1) { GPUman->compAbort(ERROR_GPUMANAGER_COMPNOTIMPLEMENTED); } unsigned int strlen = mxGetM(prhs[0]); if (mxGetN(prhs[0]) > strlen) strlen = mxGetN(prhs[0]); char *modname = (char*) Mymalloc((strlen+1)*sizeof(char),&mygc); memset(modname,0,strlen+1); mxGetString(prhs[0], modname, strlen+1); strlen = mxGetM(prhs[1]); if (mxGetN(prhs[1]) > strlen) strlen = mxGetN(prhs[1]); char *filename = (char*) Mymalloc((strlen+1)*sizeof(char),&mygc); memset(filename,0,strlen+1); mxGetString(prhs[1], filename, strlen+1); // try to load module try { GPUman->registerUserModule(modname, filename); } catch (GPUexception ex) { mexErrMsgTxt(ex.getError()); } }
void mexFunction( int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[] ) { // Garbage collector MyGC mygc = MyGC(); if (nrhs!=1) mexErrMsgTxt("Wrong number of arguments"); // tmp mxArray *lhs[2]; mexCallMATLAB(1, &lhs[0], 0, &lhs[0], "GPUmanager");\ GPUmanager * GPUman = (GPUmanager *) (UINTPTR mxGetScalar(lhs[0])); mxDestroyArray(lhs[0]); if (GPUman->getCompileMode()==1) { GPUman->compAbort(ERROR_GPUMANAGER_COMPNOTIMPLEMENTED); } unsigned int strlen = mxGetM(prhs[0]); if (mxGetN(prhs[0]) > strlen) strlen = mxGetN(prhs[0]); char *modname = (char*) Mymalloc((strlen+1)*sizeof(char),&mygc); memset(modname,0,strlen+1); mxGetString(prhs[0], modname, strlen+1); int modnumber = GPUman->getUserModuleNumberByName(modname); if (modnumber<0) mexErrMsgTxt(ERROR_GPUMANAGER_INVMODNAME); struct userModule *mymod = GPUman->getUserModule(modnumber); int nFunctions = mymod->nFunctions; mexPrintf("- User module functions summary\n"); mexPrintf(" * Found %d functions\n", nFunctions); for (int i=0;i<nFunctions;i++) { mexPrintf(" * Function %d -> %s\n", i, mymod->cuFunctionName[i]); } }
void * Myrealloc(void *b, size_t n) { ULONG *lp, l; void *nb = NULL; if(n) { lp = (ULONG *) b; l = lp[-1]; nb = Mymalloc(n); if(nb) { if(l > n) l = n; CopyMem(b, nb, l); Myfree(b); } } return nb; }
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]) { // My garbage collector MyGC mgc = MyGC(); // Garbage collector for Malloc MyGCObj<GPUtype> mgcobj; // Garbage collector for GPUtype // tmp mxArray *lhs[2]; if (nrhs<=2) mexErrMsgTxt("Wrong number of arguments"); /* Arguments * type * GPUmanager * list of dimensions */ gpuTYPE_t type = (gpuTYPE_t) ((int) mxGetScalar(prhs[0])); GPUmanager *GPUman = (GPUmanager *) (UINTPTR mxGetScalar(prhs[1])); // Make the following test // 1) complex elements // 2) should be either a scalar or a vector with dimension 2 // 3) If there are more arguments, each argument is checked to be scalar afterwards int nrhsstart = 2; // first 2 arguments are type and GPUmanager int nrhseff = nrhs-2; for (int i = nrhsstart; i < nrhs; i++) { if (mxIsComplex(prhs[i]) || (mxGetNumberOfDimensions(prhs[i]) != 2) || (mxGetM(prhs[i]) != 1)) mexErrMsgTxt("Size vector must be a row vector with real elements."); } int *mysize = NULL; int ndims; if (nrhseff == 1) { if (mxGetNumberOfElements(prhs[nrhsstart]) == 1) { mysize = (int*) Mymalloc(2 * sizeof(int),&mgc); mysize[0] = (int) mxGetScalar(prhs[nrhsstart]); mysize[1] = mysize[0]; ndims = 2; } else { int n = mxGetNumberOfElements(prhs[nrhsstart]); double *tmp = mxGetPr(prhs[nrhsstart]); mysize = (int*) Mymalloc(n * sizeof(int),&mgc); for (int i = 0; i < n; i++) { mysize[i] = (int) floor(tmp[i]); } ndims = n; } } else { int n = nrhseff; mysize = (int*) Mymalloc(n * sizeof(int),&mgc); for (int i = nrhsstart; i < nrhs; i++) { if (mxGetNumberOfElements(prhs[i]) == 1) { mysize[i-nrhsstart] = (int) mxGetScalar(prhs[i]); } else { mexErrMsgTxt("Input arguments must be scalar."); } } ndims = n; } if (mysize == NULL) mexErrMsgTxt("Unexpected error in GPUtypeNew."); // Check all dimensions different from 0 for (int i = 0; i < ndims; i++) { if (mysize[i] == 0) mexErrMsgTxt("Dimension cannot be zero."); } // remove any one at the end int finalndims = ndims; for (int i = ndims - 1; i > 1; i--) { if (mysize[i] == 1) finalndims--; else break; } ndims = finalndims; GPUtype *r = new GPUtype(type, ndims , mysize, GPUman); mgcobj.setPtr(r); // should delete this pointer r->setSize(ndims, mysize); try { GPUopAllocVector(*r); } catch (GPUexception ex) { mexErrMsgTxt(ex.getError()); } // create output result mgcobj.remPtr(r); plhs[0] = toMx(r); }
struct polynomial *Create_poly(char In_str[], int *In_str_len, int *Out_of_memory) { /*static struct unexp_tnode *temp;*/ struct unexp_tnode *Unexp_tree1; struct unexp_tnode *Unexp_tree2; struct unexp_tnode *Exp_tree1; struct unexp_tnode *Exp_tree2; struct unexp_tnode *Exp_tree3; struct unexp_tnode *Exp_tree4; struct polynomial *poly; char *exp_poly_str; static char **Exp_poly_str_ptr; /* Exp_poly_str may be expanded! */ static int Maxsize_exp_poly_str; int Modified = FALSE; /* Has tree been changed in this call? */ #if DEBUG_EXP int count; /* Debugging information. No of calls to Simplify */ #endif /*int i;*/ int first_time = TRUE; /* Allocate space for exp_poly_str first_time */ if (first_time) { exp_poly_str = (char*) Mymalloc(EXP_STR_LEN); Maxsize_exp_poly_str = EXP_STR_LEN; first_time = FALSE; } exp_poly_str[0] = '\0'; Exp_poly_str_ptr = &exp_poly_str; Unexp_tree1 = NULL; Unexp_tree2 = NULL; Exp_tree1 = NULL; Exp_tree2 = NULL; Exp_tree3 = NULL; Exp_tree4 = NULL; if (setjmp(env) != 0) { Free_tnode_tree(Unexp_tree1); Free_tnode_tree(Unexp_tree2); Free_tnode_tree(Exp_tree1); Free_tnode_tree(Exp_tree2); Free_tnode_tree(Exp_tree3); Free_tnode_tree(Exp_tree4); *Out_of_memory = TRUE; printf("Command unsuccessfull.\n"); return(NULL); } /* Parse tree for the polynomial string In_str entered by user */ Unexp_tree1 = Create_parse_tree(In_str); if (Unexp_tree1 == NULL) return(NULL); #if DEBUG_EXP Print_tree(Unexp_tree1); printf("\n"); #endif /* Expand the Parse tree by using formulae for operators */ Unexp_tree2 = Expand_parse_tree(Unexp_tree1); #if DEBUG_EXP Print_tree(Unexp_tree2); printf("\n"); #endif /* Simplify the Expanded tree i.e apply distributive laws. */ Exp_tree1 = Simplify_parse_tree(Unexp_tree2,&Modified); #if DEBUG_EXP count = 1; printf("simplified tree %d\n",count); Print_tree(Exp_tree1); printf("\n"); #endif while (Modified) { Modified = FALSE; Exp_tree2 = Simplify_parse_tree(Exp_tree1,&Modified); #if DEBUG_EXP count++; printf("count = %d Modified = %d\n",count,Modified); printf("simplified tree\n"); Print_tree(Exp_tree2); printf("\n"); printf("freeing Exp_tree1\n"); #endif Free_tnode_tree(Exp_tree1); Exp_tree1 = Exp_tree2; Exp_tree2 = NULL; } /* No more simplifications needed! */ Modified = FALSE; /* Get rid of Unary minus by multiplying that subtree by -1. */ Exp_tree3 = Elim_subtraction(Exp_tree1,&Modified); #if DEBUG_EXP count = 1; Print_tree(Exp_tree3); printf("\n"); #endif while (Modified) { Modified = FALSE; Exp_tree4 = Elim_subtraction(Exp_tree3,&Modified); #if DEBUG_EXP count++; printf("count = %d Modified = %d\n",count,Modified); Print_tree(Exp_tree4); printf("\n"); printf("freeing Exp_tree3\n"); #endif Free_tnode_tree(Exp_tree3); Exp_tree3 = Exp_tree4; Exp_tree4 = NULL; } /* Traverse the tree in inorder and write it as a string */ Create_exp_str(Exp_tree3,Exp_poly_str_ptr,&Maxsize_exp_poly_str); Free_tnode_tree(Unexp_tree1); Free_tnode_tree(Unexp_tree2); Free_tnode_tree(Exp_tree1); Free_tnode_tree(Exp_tree3); #if DEBUG_EXP printf("%s",exp_poly_str); printf("\n"); #endif /* Parse the string to create the polynomial. */ poly = Parse_exptext(*Exp_poly_str_ptr); *In_str_len = strlen(exp_poly_str); free(exp_poly_str); return(poly); }