// ============================================================================= int sci_csvTextScan(char *fname, unsigned long fname_len) { SciErr sciErr; int iErr = 0; int i = 0; int *piAddressVarOne = NULL; int m1 = 0, n1 = 0; int iType1 = 0; char **text = NULL; int *lengthText = NULL; int nbLines = 0; char *separator = NULL; char *decimal = NULL; char *conversion = NULL; double * dRealValues = NULL; int *iRange = NULL; int haveRange = 0; csvResult *result = NULL; CheckRhs(1, 5); CheckLhs(1, 1); if (Rhs == 5) { int m5 = 0, n5 = 0; iRange = csv_getArgumentAsMatrixofIntFromDouble(pvApiCtx, 5, fname, &m5, &n5, &iErr); if (iErr) { return 0; } if ((m5 * n5 != SIZE_RANGE_SUPPORTED) ) { if (iRange) { FREE(iRange); iRange = NULL; } Scierror(999, _("%s: Wrong size for input argument #%d: Four entries expected.\n"), fname, 5); return 0; } if ((m5 != 1) && (n5 != 1)) { if (iRange) { FREE(iRange); iRange = NULL; } Scierror(999, _("%s: Wrong size for input argument #%d: A column or row vector expected.\n"), fname, 5); return 0; } if (isValidRange(iRange, m5 * n5)) { haveRange = 1; } else { if (iRange) { FREE(iRange); iRange = NULL; } Scierror(999, _("%s: Wrong value for input argument #%d: Inconsistent range.\n"), fname, 5); return 0; } } if (Rhs >= 4) { conversion = csv_getArgumentAsStringWithEmptyManagement(pvApiCtx, 4, fname, getCsvDefaultConversion(), &iErr); if (iErr) { if (iRange) { FREE(iRange); iRange = NULL; } return 0; } if (!((strcmp(conversion, CONVTOSTR) == 0) || (strcmp(conversion, CONVTODOUBLE) == 0))) { if (iRange) { FREE(iRange); iRange = NULL; } if (conversion) { FREE(conversion); conversion = NULL; } Scierror(999, _("%s: Wrong value for input argument #%d: '%s' or '%s' string expected.\n"), fname, 4, "double", "string"); return 0; } } else { conversion = strdup(getCsvDefaultConversion()); } if (Rhs >= 3) { decimal = csv_getArgumentAsStringWithEmptyManagement(pvApiCtx, 3, fname, getCsvDefaultDecimal(), &iErr); if (iErr) { if (iRange) { FREE(iRange); iRange = NULL; } if (conversion) { FREE(conversion); conversion = NULL; } return 0; } if (decimal[0] != '.' && decimal[0] != ',') { if (iRange) { FREE(iRange); iRange = NULL; } if (conversion) { FREE(conversion); conversion = NULL; } Scierror(999, _("%s: Wrong value for input argument #%d: '%s' or '%s' string expected.\n"), fname, 3, ",", "."); return 0; } } else { decimal = strdup(getCsvDefaultDecimal()); } if (Rhs >= 2) { separator = csv_getArgumentAsStringWithEmptyManagement(pvApiCtx, 2, fname, getCsvDefaultSeparator(), &iErr); if (iErr) { if (iRange) { FREE(iRange); iRange = NULL; } if (decimal) { FREE(decimal); decimal = NULL; } if (conversion) { FREE(conversion); conversion = NULL; } return 0; } } else { separator = strdup(getCsvDefaultSeparator()); } if (!csv_isRowVector(pvApiCtx, 1) && !csv_isColumnVector(pvApiCtx, 1) && !csv_isScalar(pvApiCtx, 1)) { if (iRange) { FREE(iRange); iRange = NULL; } if (separator) { FREE(separator); separator = NULL; } if (decimal) { FREE(decimal); decimal = NULL; } if (conversion) { FREE(conversion); conversion = NULL; } Scierror(999, _("%s: Wrong size for input argument #%d: Vector string expected.\n"), fname, 1); return 0; } text = csv_getArgumentAsMatrixOfString(pvApiCtx, 1, fname, &m1, &n1, &iErr); if (iErr) { if (iRange) { FREE(iRange); iRange = NULL; } if (separator) { FREE(separator); separator = NULL; } if (decimal) { FREE(decimal); decimal = NULL; } if (conversion) { FREE(conversion); conversion = NULL; } return 0; } nbLines = m1 * n1; result = csvTextScan((const char**)text, nbLines, separator, decimal); if (text) { if (separator) { FREE(separator); separator = NULL; } freeArrayOfString(text, nbLines); text = NULL; } if (separator) { FREE(separator); separator = NULL; } if (result) { switch (result->err) { case CSV_READ_SEPARATOR_DECIMAL_EQUAL: { Scierror(999, _("%s: separator and decimal must have different values.\n"), fname); } break; case CSV_READ_NO_ERROR: { if (strcmp(conversion, CONVTOSTR) == 0) { if (haveRange) { int newM = 0; int newN = 0; char **pStrRange = getRangeAsString((const char**)result->pstrValues, result->m, result->n, iRange, &newM, &newN); if (pStrRange) { sciErr = createMatrixOfString(pvApiCtx, Rhs + 1, newM, newN, pStrRange); freeArrayOfString(pStrRange, newM * newN); } else { Scierror(999, _("%s: Memory allocation error.\n"), fname); } } else { sciErr = createMatrixOfString(pvApiCtx, Rhs + 1, result->m, result->n, result->pstrValues); } } else /* to double */ { stringToComplexError ierr = STRINGTOCOMPLEX_ERROR; csv_complexArray *ptrCsvComplexArray = stringsToCsvComplexArray((const char**)result->pstrValues, result->m * result->n, decimal, TRUE, &ierr); if (ptrCsvComplexArray == NULL) { freeCsvResult(result); if (decimal) { FREE(decimal); decimal = NULL; } if (conversion) { FREE(conversion); conversion = NULL; } if (iRange) { FREE(iRange); iRange = NULL; } if (ierr == STRINGTOCOMPLEX_ERROR) { Scierror(999, _("%s: can not convert data.\n"), fname); } else { Scierror(999, _("%s: Memory allocation error.\n"), fname); } return 0; } switch (ierr) { case STRINGTOCOMPLEX_NOT_A_NUMBER: case STRINGTOCOMPLEX_NO_ERROR: { if (haveRange) { int newM = 0; int newN = 0; csv_complexArray *csvComplexRange = getRangeAsCsvComplexArray(ptrCsvComplexArray, result->m, result->n, iRange, &newM, &newN); if (csvComplexRange) { if (csvComplexRange->isComplex) { sciErr = createComplexMatrixOfDouble(pvApiCtx, Rhs + 1, newM, newN, ptrCsvComplexArray->realPart, ptrCsvComplexArray->imagPart); } else { sciErr = createMatrixOfDouble(pvApiCtx, Rhs + 1, newM, newN, csvComplexRange->realPart); } freeCsvComplexArray(csvComplexRange); csvComplexRange = NULL; } else { Scierror(999, _("%s: Memory allocation error.\n"), fname); } } else { if (ptrCsvComplexArray->isComplex) { sciErr = createComplexMatrixOfDouble(pvApiCtx, Rhs + 1, result->m, result->n, ptrCsvComplexArray->realPart, ptrCsvComplexArray->imagPart); } else { sciErr = createMatrixOfDouble(pvApiCtx, Rhs + 1, result->m, result->n, ptrCsvComplexArray->realPart); } } freeCsvComplexArray(ptrCsvComplexArray); ptrCsvComplexArray = NULL; } break; case STRINGTOCOMPLEX_MEMORY_ALLOCATION: { Scierror(999, _("%s: Memory allocation error.\n"), fname); } default: case STRINGTOCOMPLEX_ERROR: { Scierror(999, _("%s: can not convert data.\n"), fname); } } } if (sciErr.iErr) { freeCsvResult(result); if (decimal) { FREE(decimal); decimal = NULL; } if (conversion) { FREE(conversion); conversion = NULL; } if (iRange) { FREE(iRange); iRange = NULL; } printError(&sciErr, 0); Scierror(17, _("%s: Memory allocation error.\n"), fname); return 0; } else { LhsVar(1) = Rhs + 1; PutLhsVar(); } } break; case CSV_READ_MEMORY_ALLOCATION: { Scierror(999, _("%s: Memory allocation error.\n"), fname); } break; case CSV_READ_COLUMNS_ERROR: { Scierror(999, _("%s: can not read text: Error in the column structure\n"), fname); } break; case CSV_READ_READLINES_ERROR: case CSV_READ_ERROR: { Scierror(999, _("%s: can not read text.\n"), fname); } break; } } else { Scierror(999, _("%s: Memory allocation error.\n"), fname); } freeCsvResult(result); if (decimal) { FREE(decimal); decimal = NULL; } if (conversion) { FREE(conversion); conversion = NULL; } if (iRange) { FREE(iRange); iRange = NULL; } return 0; }
// ============================================================================= int sci_csvStringToDouble(char *fname, unsigned long fname_len) { SciErr sciErr; int iErr = 0; int m1 = 0, n1 = 0; char **pStringValues = NULL; BOOL bConvertToNan = TRUE; complexArray *ptrComplexArray = NULL; stringToComplexError ierr = STRINGTOCOMPLEX_ERROR; CheckRhs(1, 2); CheckLhs(1, 1); if (Rhs == 1) { bConvertToNan = TRUE; } else /* Rhs == 2 */ { bConvertToNan = (BOOL)csv_getArgumentAsScalarBoolean(pvApiCtx, 2, fname, &iErr); if (iErr) { return 0; } } pStringValues = csv_getArgumentAsMatrixOfString(pvApiCtx, 1, fname, &m1, &n1, &iErr); if (iErr) { return 0; } ptrComplexArray = stringsToComplexArray((const char**)pStringValues, m1 * n1, getCsvDefaultDecimal(), bConvertToNan, &ierr); freeArrayOfString(pStringValues, m1 * n1); pStringValues = NULL; if (ptrComplexArray == NULL) { switch (ierr) { case STRINGTOCOMPLEX_NOT_A_NUMBER: case STRINGTOCOMPLEX_ERROR: Scierror(999, _("%s: can not convert data.\n"), fname); return 0; default: Scierror(999, _("%s: Memory allocation error.\n"), fname); return 0; } } switch (ierr) { case STRINGTOCOMPLEX_NOT_A_NUMBER: case STRINGTOCOMPLEX_NO_ERROR: { if (ptrComplexArray->isComplex) { sciErr = createComplexMatrixOfDouble(pvApiCtx, Rhs + 1, m1, n1, ptrComplexArray->realPart, ptrComplexArray->imagPart); } else { sciErr = createMatrixOfDouble(pvApiCtx, Rhs + 1, m1, n1, ptrComplexArray->realPart); } freeComplexArray(ptrComplexArray); ptrComplexArray = NULL; } break; case STRINGTOCOMPLEX_MEMORY_ALLOCATION: { Scierror(999, _("%s: Memory allocation error.\n"), fname); } default: case STRINGTOCOMPLEX_ERROR: { Scierror(999, _("%s: can not convert data.\n"), fname); } } if (sciErr.iErr) { printError(&sciErr, 0); } else { LhsVar(1) = Rhs + 1; PutLhsVar(); } return 0; }
/* ==================================================================== */ int sci_csvRead(char *fname, unsigned long fname_len) { SciErr sciErr; int iErr = 0; int iErrEmpty = 0; char *filename = NULL; char *separator = NULL; char *decimal = NULL; char *conversion = NULL; int *iRange = NULL; int haveRange = 0; int header = 0; char **toreplace = NULL; int nbElementsToReplace = 0; char *regexp = NULL; int haveRegexp = 0; csvResult *result = NULL; double *dRealValues = NULL; sciErr.iErr = 0; CheckRhs(1, 8); CheckLhs(1, 2); if (Rhs == 8) { header = (int) csv_getArgumentAsScalarDouble(pvApiCtx, 8, fname, &iErr); if (iErr) { freeVar(&filename, &separator, &decimal, &conversion, &iRange, &toreplace, 0, ®exp); return 0; } } if (Rhs == 7) { int m7 = 0, n7 = 0; iRange = csv_getArgumentAsMatrixofIntFromDouble(pvApiCtx, 7, fname, &m7, &n7, &iErr); if (iErr) { freeVar(&filename, &separator, &decimal, &conversion, &iRange, &toreplace, 0, ®exp); return 0; } if ((m7 * n7 != SIZE_RANGE_SUPPORTED) ) { freeVar(&filename, &separator, &decimal, &conversion, &iRange, &toreplace, 0, ®exp); Scierror(999, _("%s: Wrong size for input argument #%d: Four entries expected.\n"), fname, 7); return 0; } if ((m7 != 1) && (n7 != 1)) { freeVar(&filename, &separator, &decimal, &conversion, &iRange, &toreplace, 0, ®exp); Scierror(999, _("%s: Wrong size for input argument #%d: A column or row vector expected.\n"), fname, 7); return 0; } if (isValidRange(iRange, m7 * n7)) { haveRange = 1; } else { freeVar(&filename, &separator, &decimal, &conversion, &iRange, &toreplace, 0, ®exp); Scierror(999, _("%s: Wrong value for input argument #%d: Inconsistent range.\n"), fname, 7); return 0; } } if (Rhs >= 6) { regexp = csv_getArgumentAsStringWithEmptyManagement(pvApiCtx, 6, fname, getCsvDefaultCommentsRegExp(), &iErr); if (regexp) { if (strcmp(regexp, "") == 0) { FREE(regexp); regexp = NULL; } else { haveRegexp = 1; } } if (iErr) { freeVar(&filename, &separator, &decimal, &conversion, &iRange, &toreplace, 0, ®exp); return 0; } } else { regexp = strdup(getCsvDefaultCommentsRegExp()); if (regexp) { if (strcmp(regexp, "") == 0) { FREE(regexp); regexp = NULL; } } } if (Rhs >= 5) { if (csv_isEmpty(pvApiCtx, 5)) { toreplace = NULL; nbElementsToReplace = 0; } else { int m5 = 0, n5 = 0; toreplace = csv_getArgumentAsMatrixOfString(pvApiCtx, 5, fname, &m5, &n5, &iErr); if (iErr) { freeVar(&filename, &separator, &decimal, &conversion, &iRange, &toreplace, m5 * n5, ®exp); return 0; } if (n5 != 2) { freeVar(&filename, &separator, &decimal, &conversion, &iRange, &toreplace, m5 * n5, ®exp); Scierror(999, _("%s: Wrong size for input argument #%d.\n"), fname, 5); return 0; } nbElementsToReplace = m5; } } else { toreplace = NULL; nbElementsToReplace = 0; } if (Rhs >= 4) { int iErr = 0; conversion = csv_getArgumentAsStringWithEmptyManagement(pvApiCtx, 4, fname, getCsvDefaultConversion(), &iErr); if (iErr) { freeVar(&filename, &separator, &decimal, &conversion, &iRange, &toreplace, nbElementsToReplace, ®exp); return 0; } if (!((strcmp(conversion, CONVTOSTR) == 0) || (strcmp(conversion, CONVTODOUBLE) == 0))) { freeVar(&filename, &separator, &decimal, &conversion, &iRange, &toreplace, nbElementsToReplace, ®exp); Scierror(999, _("%s: Wrong value for input argument #%d: '%s' or '%s' string expected.\n"), fname, 4, "double", "string"); return 0; } } else { /* read_csv is using a 'string' conversion while csvRead is doing a 'double' conversion */ if (strcmp(fname, "read_csv") == 0) { conversion = (char*)MALLOC((strlen("string") + 1) * sizeof(char)); strcpy(conversion, "string"); } else { conversion = strdup(getCsvDefaultConversion()); } } if (Rhs >= 3) { int iErr = 0; decimal = csv_getArgumentAsStringWithEmptyManagement(pvApiCtx, 3, fname, getCsvDefaultDecimal(), &iErr); if (iErr) { freeVar(&filename, &separator, &decimal, &conversion, &iRange, &toreplace, nbElementsToReplace, ®exp); return 0; } } else { decimal = strdup(getCsvDefaultDecimal()); } if (Rhs >= 2) { int iErr = 0; separator = csv_getArgumentAsStringWithEmptyManagement(pvApiCtx, 2, fname, getCsvDefaultSeparator(), &iErr); if (iErr) { freeVar(&filename, &separator, &decimal, &conversion, &iRange, &toreplace, nbElementsToReplace, ®exp); return 0; } } else { separator = strdup(getCsvDefaultSeparator()); } if (strcmp(separator, "\\t") == 0) { /* In Scilab, if the user is providing \t as separator, transform it to a real tab. Example: read_csv(filename,"\t"); */ strcpy(separator, "\t"); } filename = csv_getArgumentAsString(pvApiCtx, 1, fname, &iErr); if (iErr) { freeVar(&filename, &separator, &decimal, &conversion, &iRange, &toreplace, nbElementsToReplace, ®exp); return 0; } result = csvRead(filename, separator, decimal, (const char**)toreplace, nbElementsToReplace * 2, regexp, header); freeVar(NULL, &separator, &decimal, NULL, NULL, &toreplace, nbElementsToReplace, ®exp); if (result) { switch (result->err) { case CSV_READ_REGEXP_ERROR: { Scierror(999, _("%s: Wrong value for input argument #%d.\n"), fname, 6); } break; case CSV_READ_SEPARATOR_DECIMAL_EQUAL: { Scierror(999, _("%s: separator and decimal must have different values.\n"), fname); } break; case CSV_READ_NO_ERROR: { if (strcmp(conversion, CONVTOSTR) == 0) { if (haveRange) { int newM = 0; int newN = 0; char **pStrRange = getRangeAsString((const char**)result->pstrValues, result->m, result->n, iRange, &newM, &newN); if (pStrRange) { sciErr = createMatrixOfString(pvApiCtx, Rhs + 1, newM, newN, pStrRange); freeArrayOfString(pStrRange, newM * newN); } else { if ((newM == 0) || (newN == 0)) { Scierror(999, _("%s: Range row or/and column left indice(s) out of bounds.\n"), fname); } else { Scierror(999, _("%s: Memory allocation error.\n"), fname); } freeCsvResult(result); freeVar(&filename, &separator, &decimal, &conversion, &iRange, &toreplace, nbElementsToReplace, ®exp); return 0; } } else { sciErr = createMatrixOfString(pvApiCtx, Rhs + 1, result->m, result->n, result->pstrValues); } } else /* to double */ { stringToComplexError ierr = STRINGTOCOMPLEX_ERROR; complexArray *ptrComplexArray = stringsToComplexArray((const char**)result->pstrValues, result->m * result->n, decimal, TRUE, &ierr); if (ptrComplexArray == NULL) { freeCsvResult(result); if (ierr == STRINGTOCOMPLEX_ERROR) { Scierror(999, _("%s: can not convert data.\n"), fname); } else { Scierror(999, _("%s: Memory allocation error.\n"), fname); } freeVar(&filename, &separator, &decimal, &conversion, &iRange, &toreplace, nbElementsToReplace, ®exp); return 0; } switch (ierr) { case STRINGTOCOMPLEX_NOT_A_NUMBER: case STRINGTOCOMPLEX_NO_ERROR: { if (haveRange) { int newM = 0; int newN = 0; complexArray *complexRange = getRangeAsComplexArray(ptrComplexArray, result->m, result->n, iRange, &newM, &newN); if (complexRange) { if (complexRange->isComplex) { sciErr = createComplexMatrixOfDouble(pvApiCtx, Rhs + 1, newM, newN, ptrComplexArray->realPart, ptrComplexArray->imagPart); } else { sciErr = createMatrixOfDouble(pvApiCtx, Rhs + 1, newM, newN, complexRange->realPart); } freeComplexArray(complexRange); complexRange = NULL; } else { if ((newM == 0) || (newN == 0)) { Scierror(999, _("%s: Range row or/and column left indice(s) out of bounds.\n"), fname); } else { Scierror(999, _("%s: Memory allocation error.\n"), fname); } freeCsvResult(result); freeVar(&filename, &separator, &decimal, &conversion, &iRange, &toreplace, nbElementsToReplace, ®exp); return 0; } } else { if (ptrComplexArray->isComplex) { sciErr = createComplexMatrixOfDouble(pvApiCtx, Rhs + 1, result->m, result->n, ptrComplexArray->realPart, ptrComplexArray->imagPart); } else { sciErr = createMatrixOfDouble(pvApiCtx, Rhs + 1, result->m, result->n, ptrComplexArray->realPart); } freeComplexArray(ptrComplexArray); ptrComplexArray = NULL; } } break; case STRINGTOCOMPLEX_MEMORY_ALLOCATION: { Scierror(999, _("%s: Memory allocation error.\n"), fname); freeCsvResult(result); freeVar(&filename, &separator, &decimal, &conversion, &iRange, &toreplace, nbElementsToReplace, ®exp); return 0; } default: case STRINGTOCOMPLEX_ERROR: { Scierror(999, _("%s: can not convert data.\n"), fname); freeCsvResult(result); freeVar(&filename, &separator, &decimal, &conversion, &iRange, &toreplace, nbElementsToReplace, ®exp); return 0; } } } if (sciErr.iErr) { Scierror(999, _("%s: Memory allocation error.\n"), fname); freeCsvResult(result); freeVar(&filename, &separator, &decimal, &conversion, &iRange, &toreplace, nbElementsToReplace, ®exp); return 0; } else { LhsVar(1) = Rhs + 1; if (Lhs == 2) { if (haveRegexp == 0) { char **emptyStringMatrix = NULL; emptyStringMatrix = (char**) malloc(sizeof(char*)); emptyStringMatrix[0] = ""; sciErr = createMatrixOfString(pvApiCtx, Rhs + 2, 1, 1, emptyStringMatrix); free(emptyStringMatrix); } else { if (result->nbComments > 0) { sciErr = createMatrixOfString(pvApiCtx, Rhs + 2, result->nbComments, 1, result->pstrComments); } else { iErrEmpty = createEmptyMatrix(pvApiCtx, Rhs+2); sciErr.iErr = iErrEmpty; } } if (sciErr.iErr) { Scierror(999, _("%s: Memory allocation error.\n"), fname); freeCsvResult(result); freeVar(&filename, &separator, &decimal, &conversion, &iRange, &toreplace, nbElementsToReplace, ®exp); return 0; } LhsVar(2) = Rhs + 2; } PutLhsVar(); } } break; case CSV_READ_FILE_NOT_EXIST: { Scierror(999, _("%s: %s does not exist.\n"), fname, filename); } break; case CSV_READ_MOPEN_ERROR: { Scierror(999, _("%s: can not open file %s.\n"), fname, filename); } break; case CSV_READ_MEMORY_ALLOCATION: { Scierror(999, _("%s: Memory allocation error.\n"), fname); } break; case CSV_READ_COLUMNS_ERROR: { Scierror(999, _("%s: can not read file %s: Error in the column structure\n"), fname, filename); } break; case CSV_READ_READLINES_ERROR: case CSV_READ_ERROR: { Scierror(999, _("%s: can not read file %s.\n"), fname, filename); } break; } } else { Scierror(999, _("%s: Memory allocation error.\n"), fname); } freeCsvResult(result); freeVar(&filename, &separator, &decimal, &conversion, &iRange, &toreplace, nbElementsToReplace, ®exp); return 0; }
/* ========================================================================== */ int sci_gpuLU(char *fname) { CheckRhs(1,2); CheckLhs(2,2); #ifdef WITH_CUDA cublasStatus status; #endif SciErr sciErr; int* piAddr_A = NULL; double* h_A = NULL; double* hi_A = NULL; int rows_A; int cols_A; int* piAddr_Opt = NULL; double* option = NULL; int rows_Opt; int cols_Opt; void* d_A = NULL; int na; void* pvPtr = NULL; int size_A = sizeof(double); bool bComplex_A = FALSE; int inputType_A; int inputType_Opt; double res; int posOutput = 1; try { sciErr = getVarAddressFromPosition(pvApiCtx, 1, &piAddr_A); if(sciErr.iErr) throw sciErr; if(Rhs == 2) { sciErr = getVarAddressFromPosition(pvApiCtx, 2, &piAddr_Opt); if(sciErr.iErr) throw sciErr; sciErr = getVarType(pvApiCtx, piAddr_Opt, &inputType_Opt); if(sciErr.iErr) throw sciErr; if(inputType_Opt == sci_matrix) { sciErr = getMatrixOfDouble(pvApiCtx, piAddr_Opt, &rows_Opt, &cols_Opt, &option); if(sciErr.iErr) throw sciErr; } else throw "Option syntax is [number,number]."; } else { rows_Opt=1; cols_Opt=2; option = (double*)malloc(2*sizeof(double)); option[0]=0; option[1]=0; } if(rows_Opt != 1 || cols_Opt != 2) throw "Option syntax is [number,number]."; if((int)option[1] == 1 && !isGpuInit()) throw "gpu is not initialised. Please launch gpuInit() before use this function."; sciErr = getVarType(pvApiCtx, piAddr_A, &inputType_A); if(sciErr.iErr) throw sciErr; #ifdef WITH_CUDA if (useCuda()) { if(inputType_A == sci_pointer) { sciErr = getPointer(pvApiCtx, piAddr_A, (void**)&pvPtr); if(sciErr.iErr) throw sciErr; gpuMat_CUDA* gmat; gmat = static_cast<gpuMat_CUDA*>(pvPtr); if(!gmat->useCuda) throw "Please switch to OpenCL mode before use this data."; rows_A=gmat->rows; cols_A=gmat->columns; if(gmat->complex) { bComplex_A = TRUE; size_A = sizeof(cuDoubleComplex); d_A=(cuDoubleComplex*)gmat->ptr->get_ptr(); } else d_A=(double*)gmat->ptr->get_ptr(); // Initialize CUBLAS status = cublasInit(); if (status != CUBLAS_STATUS_SUCCESS) throw status; na = rows_A * cols_A; } else if(inputType_A == 1) { // Get size and data if(isVarComplex(pvApiCtx, piAddr_A)) { sciErr = getComplexMatrixOfDouble(pvApiCtx, piAddr_A, &rows_A, &cols_A, &h_A, &hi_A); if(sciErr.iErr) throw sciErr; size_A = sizeof(cuDoubleComplex); bComplex_A = TRUE; } else { sciErr = getMatrixOfDouble(pvApiCtx, piAddr_A, &rows_A, &cols_A, &h_A); if(sciErr.iErr) throw sciErr; } na = rows_A * cols_A; // Initialize CUBLAS status = cublasInit(); if (status != CUBLAS_STATUS_SUCCESS) throw status; // Allocate device memory status = cublasAlloc(na, size_A, (void**)&d_A); if (status != CUBLAS_STATUS_SUCCESS) throw status; // Initialize the device matrices with the host matrices if(!bComplex_A) { status = cublasSetMatrix(rows_A,cols_A, sizeof(double), h_A, rows_A, (double*)d_A, rows_A); if (status != CUBLAS_STATUS_SUCCESS) throw status; } else writecucomplex(h_A, hi_A, rows_A, cols_A, (cuDoubleComplex *)d_A); } else throw "Bad argument type."; cuDoubleComplex resComplex; // Performs operation if(!bComplex_A) status = decomposeBlockedLU(rows_A, cols_A, rows_A, (double*)d_A, 1); // else // resComplex = cublasZtrsm(na,(cuDoubleComplex*)d_A); if (status != CUBLAS_STATUS_SUCCESS) throw status; // Put the result in scilab switch((int)option[0]) { case 2 : case 1 : sciprint("The first option must be 0 for this function. Considered as 0.\n"); case 0 : // Keep the result on the Host. { // Put the result in scilab if(!bComplex_A) { double* h_res = NULL; sciErr=allocMatrixOfDouble(pvApiCtx, Rhs + posOutput, rows_A, cols_A, &h_res); if(sciErr.iErr) throw sciErr; status = cublasGetMatrix(rows_A,cols_A, sizeof(double), (double*)d_A, rows_A, h_res, rows_A); if (status != CUBLAS_STATUS_SUCCESS) throw status; } else { sciErr = createComplexMatrixOfDouble(pvApiCtx, Rhs + posOutput, 1, 1, &resComplex.x,&resComplex.y); if(sciErr.iErr) throw sciErr; } LhsVar(posOutput)=Rhs+posOutput; posOutput++; break; } default : throw "First option argument must be 0 or 1 or 2."; } switch((int)option[1]) { case 0 : // Don't keep the data input on Device. { if(inputType_A == sci_matrix) { status = cublasFree(d_A); if (status != CUBLAS_STATUS_SUCCESS) throw status; d_A = NULL; } break; } case 1 : // Keep data of the fisrt argument on Device and return the Device pointer. { if(inputType_A == sci_matrix) { gpuMat_CUDA* dptr; gpuMat_CUDA tmp={getCudaContext()->genMatrix<double>(getCudaQueue(),rows_A*cols_A),rows_A,cols_A}; dptr=new gpuMat_CUDA(tmp); dptr->useCuda = true; dptr->ptr->set_ptr((double*)d_A); if(bComplex_A) dptr->complex=TRUE; else dptr->complex=FALSE; sciErr = createPointer(pvApiCtx,Rhs+posOutput, (void*)dptr); if(sciErr.iErr) throw sciErr; LhsVar(posOutput)=Rhs+posOutput; } else throw "The first input argument is already a GPU variable."; posOutput++; break; } default : throw "Second option argument must be 0 or 1."; } // Shutdown status = cublasShutdown(); if (status != CUBLAS_STATUS_SUCCESS) throw status; } #endif #ifdef WITH_OPENCL if (!useCuda()) { throw "not implemented with OpenCL."; } #endif if(Rhs == 1) { free(option); option = NULL; } if(posOutput < Lhs+1) throw "Too many output arguments."; if(posOutput > Lhs+1) throw "Too few output arguments."; PutLhsVar(); return 0; } catch(const char* str) { Scierror(999,"%s\n",str); } catch(SciErr E) { printError(&E, 0); } #ifdef WITH_CUDA catch(cudaError_t cudaE) { GpuError::treat_error<CUDAmode>((CUDAmode::Status)cudaE); } catch(cublasStatus CublasE) { GpuError::treat_error<CUDAmode>((CUDAmode::Status)CublasE,1); } if (useCuda()) { if(inputType_A == 1 && d_A != NULL) cudaFree(d_A); } #endif #ifdef WITH_OPENCL if (!useCuda()) { Scierror(999,"not implemented with OpenCL.\n"); } #endif if(Rhs == 1 && option != NULL) free(option); return EXIT_FAILURE; }
int read_double(char *fname,unsigned long fname_len) { SciErr sciErr; int i; //first variable info : real matrix of double int iType = 0; int iRows = 0; int iCols = 0; int iComplex = 0; int *piAddr = NULL; double* pdblReal = NULL; double* pdblImg = NULL; //check input and output arguments CheckInputArgument(pvApiCtx, 1, 1); CheckOutputArgument(pvApiCtx, 1, 1); /************************ * First variable * ************************/ //get variable address of the first input argument sciErr = getVarAddressFromPosition(pvApiCtx, 1, &piAddr); if(sciErr.iErr) { printError(&sciErr, 0); return 0; } //check type sciErr = getVarType(pvApiCtx, piAddr, &iType); if(sciErr.iErr || iType != sci_matrix) { printError(&sciErr, 0); return 0; } //get complexity iComplex = isVarComplex(pvApiCtx, piAddr); //check complexity if(iComplex) { //get size and data from Scilab memory sciErr = getComplexMatrixOfDouble(pvApiCtx, piAddr, &iRows, &iCols, &pdblReal, &pdblImg); } else { //get size and data from Scilab memory sciErr = getMatrixOfDouble(pvApiCtx, piAddr, &iRows, &iCols, &pdblReal); } if(sciErr.iErr) { printError(&sciErr, 0); return 0; } //Do something with data //if variable is complex, switch real part and imaginary part otherwise multiply by -1 if(iComplex) { sciErr = createComplexMatrixOfDouble(pvApiCtx, InputArgument + 1, iRows, iCols, pdblImg, pdblReal); } else { for(i = 0 ; i < iRows * iCols ; i++) { pdblReal[i] = pdblReal[i] * -1; } sciErr = createMatrixOfDouble(pvApiCtx, InputArgument + 1, iRows, iCols, pdblReal); } if(sciErr.iErr) { printError(&sciErr, 0); return 0; } AssignOutputVariable(1) = InputArgument + 1; return 0; }