Exemplo n.º 1
0
extern "C" int sci_sdpa_read_prob(char * fname)
{
  int nb_vars, nb_constr, i, j, k, Index;
  struct blockmatrix pC;
  double * pa = NULL;
  struct constraintmatrix * pconstraints = NULL;
  struct sparseblock * tmp_block = NULL;
  int printlevel = 0, ret = 0, res = 0;
  char ** filename = NULL;
  int * filename_address = NULL, filename_rows, filename_cols, * filename_length = NULL;
  int * printlevel_address = NULL, printlevel_rows, printlevel_cols;
  double * printlevel_value = NULL, * c_out = NULL, * b_out = NULL;
  int * c_out_address = NULL, * a_out_address = NULL;
  int * constraint_address = NULL;
  int constraint_block_rows, constraint_block_cols, constraint_nb_item;
  double * block_value = NULL;
  int minrhs = 1, maxrhs = 2;
  int minlhs = 4, maxlhs = 4;
  SciErr sciErr;

  CheckRhs(minrhs,maxrhs);
  CheckLhs(minlhs,maxlhs);

  ///////////////////////
  // Read the filename //
  ///////////////////////

  sciErr = getVarAddressFromPosition(pvApiCtx,FILENAME_IN,&filename_address); CSDP_ERROR;
  sciErr = getMatrixOfString(pvApiCtx,filename_address, &filename_rows, &filename_cols, NULL, NULL); CSDP_ERROR;
  if (filename_rows*filename_cols!=1)
    {
      Scierror(999,"%s: an unique string is requested for the filename\n",fname);
      return 0;
    }
  filename_length = (int *)MALLOC(filename_rows*filename_cols*sizeof(int));
  sciErr = getMatrixOfString(pvApiCtx,filename_address, &filename_rows, &filename_cols, filename_length, NULL); CSDP_ERROR;
  filename = (char **)MALLOC(filename_rows*filename_cols*sizeof(char *));
  for(i=0;i<filename_rows*filename_cols;i++)
    {
      filename[i] = (char *)MALLOC((filename_length[i]+1)*sizeof(char));
    }
  sciErr = getMatrixOfString(pvApiCtx,filename_address, &filename_rows, &filename_cols, filename_length, filename); CSDP_ERROR;

  /////////////////////
  // Read printlevel //
  /////////////////////

  if (Rhs==2)
    {
      sciErr = getVarAddressFromPosition(pvApiCtx,PRINTLEVEL_IN, &printlevel_address); CSDP_ERROR;
      sciErr = getMatrixOfDouble(pvApiCtx,printlevel_address, &printlevel_rows, &printlevel_cols, &printlevel_value); CSDP_ERROR;
      printlevel = (int)*printlevel_value;
    }
  else
    {
      printlevel = 0;
    }

  ///////////////////////////
  // Read the sdpa problem //
  ///////////////////////////

  ret =  read_prob(filename[0], &nb_vars, &nb_constr, &pC, &pa, &pconstraints, printlevel);

#ifdef DEBUG
  printf("DEBUG: read file %s, return code = %d nb_vars = %d nb_constr = %d\n", filename[0], ret, nb_vars, nb_constr);
#endif

  ///////////////
  // Process C //
  ///////////////

  sciErr = createList(pvApiCtx,C_OUT,pC.nblocks,&c_out_address); CSDP_ERROR;

#ifdef DEBUG
  printf("DEBUG: process c: %d blocks\n", pC.nblocks);
#endif

  for(i=0;i<pC.nblocks;i++)
    {
#ifdef DEBUG
      printf("DEBUG: processing block %d: blockssize = %d blockcat = %d\n", i+1, pC.blocks[i+1].blocksize, pC.blocks[i+1].blockcategory);
#endif
      if (pC.blocks[i+1].blockcategory==MATRIX)
	{
	  sciErr = allocMatrixOfDoubleInList(pvApiCtx, C_OUT, c_out_address, i+1, pC.blocks[i+1].blocksize, pC.blocks[i+1].blocksize, &c_out); CSDP_ERROR;
	  
	  for(j=0;j<pC.blocks[i+1].blocksize;j++)
	    {
	      for(k=0;k<pC.blocks[i+1].blocksize;k++)
		{
		  c_out[j + k*pC.blocks[i+1].blocksize] = pC.blocks[i+1].data.mat[ijtok(j+1,k+1,pC.blocks[i+1].blocksize)];
#ifdef DEBUG
		  printf("DEBUG: MATRIX - c_out_m[%d][%d] = %f - Index = %d\n",j,k,c_out[j + k*pC.blocks[i+1].blocksize], ijtok(j+1,k+1,pC.blocks[i+1].blocksize));
#endif
		}
	    }
	}
      else if (pC.blocks[i+1].blockcategory==DIAG)
	{
	  sciErr = allocMatrixOfDoubleInList(pvApiCtx, C_OUT, c_out_address, i+1, pC.blocks[i+1].blocksize, 1, &c_out); CSDP_ERROR;
	  
	  for(j=0;j<pC.blocks[i+1].blocksize;j++)
	    {
	      c_out[j] = pC.blocks[i+1].data.vec[j+1];
#ifdef DEBUG
	      printf("DEBUG: DIAG - c_out_d[%d] = %f\n",j,c_out[j]);
#endif
	    }	  
	}
      else
	{
	  Scierror(999,"%s: wrong blockcat type PACKEDMATRIX\n",fname);
	  return 0;
	}
    }

  ///////////////
  // Process A //
  ///////////////

#ifdef DEBUG
  printf("DEBUG: process a - nb_constr = %d\n",nb_constr);
#endif
  sciErr = createList(pvApiCtx,A_OUT,nb_constr,&a_out_address); CSDP_ERROR;

  for(i=0;i<nb_constr;i++)
    {
#ifdef DEBUG
      printf("DEBUG: processing constraint %d\n", i+1);
#endif
      // constraint_nb_item: nb blocks for constraint i+1
      Index = 0;
      tmp_block = pconstraints[i+1].blocks;
      while (tmp_block)
	{
	  tmp_block = tmp_block->next;
	  Index++;
	}
      constraint_nb_item = Index;

      sciErr = createListInList(pvApiCtx,A_OUT, a_out_address, i+1, constraint_nb_item, &constraint_address); CSDP_ERROR;

      Index = 0;
      tmp_block = pconstraints[i+1].blocks;
      for(j=0;j<constraint_nb_item;j++)
	{
#ifdef DEBUG
	  printf("DEBUG: processing block %d: nb_block = %d blocksize = %d numentries %d\n", j+1, constraint_nb_item, tmp_block->blocksize, tmp_block->numentries);
	  printf("DEBUG:                      blocknum = %d constraintnum = %d issparse = %d\n", tmp_block->blocknum, tmp_block->constraintnum, tmp_block->issparse);
#endif
	  constraint_block_rows = tmp_block->blocksize;
	  constraint_block_cols = tmp_block->blocksize;
#ifdef DEBUG
	  int ii;
	  for(ii=0;ii<tmp_block->numentries;ii++)
	    {
	      printf("entries[%d] = %f\n",ii+1,tmp_block->entries[ii+1]);
	    }
#endif
	  sciErr = allocMatrixOfDoubleInList(pvApiCtx, A_OUT, constraint_address, j+1, constraint_block_rows, constraint_block_cols, &block_value); CSDP_ERROR;
	  if (res)
	    {
	      Scierror(999,"%s: error while allocating a matrix A on the stack\n", fname);
	      return 0;
	    }
	  for(k=0;k<constraint_block_rows*constraint_block_cols;k++) block_value[k] = 0.0;

	  for(k=0;k<tmp_block->numentries;k++)
	    {
	      block_value[(tmp_block->iindices[k+1]-1) + (tmp_block->jindices[k+1]-1)*tmp_block->blocksize] = tmp_block->entries[k+1];
#ifdef DEBUG
	      printf("a_out[%d][%d] = %f\n",tmp_block->iindices[k+1],tmp_block->jindices[k+1],
			block_value[(tmp_block->iindices[k+1]-1)+(tmp_block->jindices[k+1]-1)*tmp_block->blocksize]);
#endif
	    }
	  tmp_block = tmp_block->next;
	}
    }

  ///////////////
  // Process B //
  ///////////////

#ifdef DEBUG
  printf("DEBUG: process B\n");
#endif
  sciErr = allocMatrixOfDouble(pvApiCtx, B_OUT, nb_constr, 1, &b_out); CSDP_ERROR;
  if (res)
    {
      Scierror(999,"%s: error while allocating a vector on the stack\n", fname);
      return 0;
    }
  for(i=0;i<nb_constr;i++) 
    {
      b_out[i] = pa[i+1];
#ifdef DEBUG
      printf("a[%d] = %f\n", i, b_out[i]);
#endif
    }

  ////////////////////
  // Process status //
  ////////////////////

#ifdef DEBUG
  printf("DEBUG: process status\n");
#endif
  sciErr = allocMatrixOfDouble(pvApiCtx, STATUS_OUT, 1, 1, &b_out); CSDP_ERROR;
  *b_out = (double)ret;
#ifdef DEBUG
  printf("DEBUG: status = %d,%d\n",*b_out,ret);
#endif

  LhsVar(1) = C_OUT;
  LhsVar(2) = A_OUT;
  LhsVar(3) = B_OUT;
  LhsVar(4) = STATUS_OUT;

  return 0;
}
Exemplo n.º 2
0
int sci_hdf5_listvar_v2(char *fname, int* pvApiCtx)
{
    SciErr sciErr;
    int *piAddr     = NULL;
    char* pstFile   = NULL;
    int iFile       = 0;
    int iNbItem     = 0;
    VarInfo* pInfo  = NULL;
    const int nbIn = nbInputArgument(pvApiCtx);

    CheckInputArgument(pvApiCtx, 1, 1);
    CheckOutputArgument(pvApiCtx, 1, 4);

    sciErr = getVarAddressFromPosition(pvApiCtx, 1, &piAddr);
    if (sciErr.iErr)
    {
        printError(&sciErr, 0);
        return 1;
    }

    if (getAllocatedSingleString(pvApiCtx, piAddr, &pstFile))
    {
        if (pstFile)
        {
            FREE(pstFile);
        }

        Scierror(999, _("%s: Wrong size for input argument #%d: A string expected.\n"), fname, 1);
        return 1;
    }

    char* pstFileName = expandPathVariable(pstFile);
    iFile = openHDF5File(pstFileName, 0);
    if (iFile < 0)
    {
        Scierror(999, _("%s: Unable to open file: %s\n"), fname, pstFile);
        FREE(pstFileName);
        FREE(pstFile);
        return 1;
    }

    FREE(pstFileName);
    FREE(pstFile);

    //manage version information
    int iVersion = getSODFormatAttribute(iFile);
    if (iVersion != SOD_FILE_VERSION)
    {
        if (iVersion > SOD_FILE_VERSION)
        {
            //can't read file with version newer that me !
            closeHDF5File(iFile);
            Scierror(999, _("%s: Wrong SOD file format version. Max Expected: %d Found: %d\n"), fname, SOD_FILE_VERSION, iVersion);
            return 1;
        }
        else
        {
            //call older import functions and exit or ... EXIT !
            if (iVersion == 1 || iVersion == -1)
            {
                //sciprint("old sci_listvar_in_hdf5_v1\n");
                return sci_listvar_in_hdf5_v1(fname, pvApiCtx);
            }
        }
    }

    iNbItem = getVariableNames(iFile, NULL);
    if (iNbItem != 0)
    {
        char** pstVarNameList = (char**)MALLOC(sizeof(char*) * iNbItem);
        pInfo = (VarInfo*)MALLOC(iNbItem * sizeof(VarInfo));
        int b;

        if (Lhs == 1)
        {
            sciprint("Name                     Type           Size            Bytes\n");
            sciprint("---------------------------------------------------------------\n");
        }

        iNbItem = getVariableNames(iFile, pstVarNameList);
        for (int i = 0; i < iNbItem; i++)
        {
            int iDataSetId = getDataSetIdFromName(iFile, pstVarNameList[i]);
            if (iDataSetId == 0)
            {
                break;
            }

            strncpy(pInfo[i].varName, pstVarNameList[i], sizeof(pInfo[i].varName) - 1);
            pInfo[i].iSize = 0;
            b = read_data(iDataSetId, 0, NULL, &pInfo[i]) == false;
            if (b)
            {
                break;
            }

            if (Lhs == 1)
            {
                sciprint("%s\n", pInfo[i].pstInfo);
            }
        }

        freeArrayOfString(pstVarNameList, iNbItem);
    }
    else
    {
        //no variable returms [] for each Lhs
        for (int i = 0 ; i < Lhs ; i++)
        {
            createEmptyMatrix(pvApiCtx, nbIn + i + 1);
            AssignOutputVariable(pvApiCtx, i + 1) = nbIn + i + 1;
        }

        ReturnArguments(pvApiCtx);
        return 0;
    }

    closeHDF5File(iFile);

    //1st Lhs
    char** pstVarName = (char**)MALLOC(sizeof(char*) * iNbItem);
    for (int i = 0 ; i < iNbItem ; i++)
    {
        pstVarName[i] = pInfo[i].varName;
    }

    sciErr = createMatrixOfString(pvApiCtx, nbIn + 1, iNbItem, 1, pstVarName);
    FREE(pstVarName);
    if (sciErr.iErr)
    {
        FREE(pInfo);
        printError(&sciErr, 0);
        return 1;
    }

    AssignOutputVariable(pvApiCtx, 1) = nbIn + 1;

    if (Lhs > 1)
    {
        //2nd Lhs
        double* pdblType;
        sciErr = allocMatrixOfDouble(pvApiCtx, nbIn + 2, iNbItem, 1, &pdblType);
        if (sciErr.iErr)
        {
            printError(&sciErr, 0);
            FREE(pInfo);
            return 1;
        }

        for (int i = 0 ; i < iNbItem ; i++)
        {
            pdblType[i] = pInfo[i].iType;
        }

        AssignOutputVariable(pvApiCtx, 2) = nbIn + 2;

        if (Lhs > 2)
        {
            //3rd Lhs
            int* pList = NULL;
            sciErr = createList(pvApiCtx, nbIn + 3, iNbItem, &pList);
            for (int i = 0 ; i < iNbItem ; i++)
            {
                double* pdblDims = NULL;
                allocMatrixOfDoubleInList(pvApiCtx, nbIn + 3, pList, i + 1, 1, pInfo[i].iDims, &pdblDims);
                for (int j = 0 ; j < pInfo[i].iDims ; j++)
                {
                    pdblDims[j] = pInfo[i].piDims[j];
                }
            }

            AssignOutputVariable(pvApiCtx, 3) = nbIn + 3;
        }

        if (Lhs > 3)
        {
            //4th Lhs
            double* pdblSize = NULL;
            sciErr = allocMatrixOfDouble(pvApiCtx, nbIn + 4, iNbItem, 1, &pdblSize);
            for (int i = 0 ; i < iNbItem ; i++)
            {
                pdblSize[i] = pInfo[i].iSize;
            }

            AssignOutputVariable(pvApiCtx, 4) = nbIn + 4;
        }

    }

    FREE(pInfo);
    ReturnArguments(pvApiCtx);
    return 0;
}
int sci_listvar_in_hdf5_v1(char *fname, int* pvCtx)
{
    SciErr sciErr;
    int *piAddr     = NULL;
    char* pstFile   = NULL;
    int iFile       = 0;
    int iNbItem     = 0;
    VarInfo_v1* pInfo  = NULL;

    CheckInputArgument(pvCtx, 1, 1);
    CheckOutputArgument(pvCtx, 1, 4);

    sciErr = getVarAddressFromPosition(pvCtx, 1, &piAddr);
    if (sciErr.iErr)
    {
        printError(&sciErr, 0);
        return 1;
    }

    if (getAllocatedSingleString(pvCtx, piAddr, &pstFile))
    {
        if (pstFile)
        {
            FREE(pstFile);
        }

        Scierror(999, _("%s: Wrong size for input argument #%d: string expected.\n"), fname, 1);
        return 1;
    }

    char* pstFileName = expandPathVariable(pstFile);
    iFile = openHDF5File(pstFileName, 0);
    if (iFile < 0)
    {
        FREE(pstFileName);
        Scierror(999, _("%s: Unable to open file: %s\n"), fname, pstFile);
        FREE(pstFile);
        return 1;
    }
    FREE(pstFileName);
    FREE(pstFile);

    iNbItem = getVariableNames_v1(iFile, NULL);
    if (iNbItem != 0)
    {
        char** pstVarNameList = (char**)MALLOC(sizeof(char*) * iNbItem);
        bool b;
        pInfo = (VarInfo_v1*)MALLOC(iNbItem * sizeof(VarInfo_v1));

        if (nbOutputArgument(pvCtx) == 1)
        {
            sciprint("Name                     Type           Size            Bytes\n");
            sciprint("---------------------------------------------------------------\n");
        }

        iNbItem = getVariableNames_v1(iFile, pstVarNameList);
        for (int i = 0; i < iNbItem; i++)
        {
            int iDataSetId = getDataSetIdFromName_v1(iFile, pstVarNameList[i]);
            if (iDataSetId == 0)
            {
                break;
            }

            strncpy(pInfo[i].varName, pstVarNameList[i], sizeof(pInfo[i].varName));
            b = read_data_v1(pvCtx, iDataSetId, 0, NULL, &pInfo[i]) == false;
            closeDataSet_v1(iDataSetId);

            if (b)
            {
                break;
            }

            if (nbOutputArgument(pvCtx) == 1)
            {
                sciprint("%s\n", pInfo[i].pstInfo);
            }
        }

        freeArrayOfString(pstVarNameList, iNbItem);
    }
    else
    {
        //no variable returms [] for each Lhs
        for (int i = 0 ; i < nbOutputArgument(pvCtx) ; i++)
        {
            createEmptyMatrix(pvCtx, nbInputArgument(pvCtx) + i + 1);
            AssignOutputVariable(pvCtx, i + 1) = nbInputArgument(pvCtx) + i + 1;
        }

        ReturnArguments(pvCtx);
        return 0;
    }

    closeHDF5File(iFile);

    //1st Lhs
    char** pstVarName = (char**)MALLOC(sizeof(char*) * iNbItem);
    for (int i = 0 ; i < iNbItem ; i++)
    {
        pstVarName[i] = pInfo[i].varName;
    }

    sciErr = createMatrixOfString(pvCtx, nbInputArgument(pvCtx) + 1, iNbItem, 1, pstVarName);
    FREE(pstVarName);
    if (sciErr.iErr)
    {
        FREE(pInfo);
        printError(&sciErr, 0);
        return 1;
    }

    AssignOutputVariable(pvCtx, 1) = nbInputArgument(pvCtx) + 1;

    if (nbOutputArgument(pvCtx) > 1)
    {
        //2nd Lhs
        double* pdblType;
        sciErr = allocMatrixOfDouble(pvCtx, nbInputArgument(pvCtx) + 2, iNbItem, 1, &pdblType);
        if (sciErr.iErr)
        {
            FREE(pInfo);
            printError(&sciErr, 0);
            return 1;
        }

        for (int i = 0 ; i < iNbItem ; i++)
        {
            pdblType[i] = pInfo[i].iType;
        }

        AssignOutputVariable(pvCtx, 2) = nbInputArgument(pvCtx) + 2;

        if (nbOutputArgument(pvCtx) > 2)
        {
            //3rd Lhs
            int* pList = NULL;
            sciErr = createList(pvCtx, nbInputArgument(pvCtx) + 3, iNbItem, &pList);
            for (int i = 0 ; i < iNbItem ; i++)
            {
                double* pdblDims = NULL;
                allocMatrixOfDoubleInList(pvCtx, nbInputArgument(pvCtx) + 3, pList, i + 1, 1, pInfo[i].iDims, &pdblDims);
                for (int j = 0 ; j < pInfo[i].iDims ; j++)
                {
                    pdblDims[j] = pInfo[i].piDims[j];
                }
            }

            AssignOutputVariable(pvCtx, 3) = nbInputArgument(pvCtx) + 3;
        }

        if (nbOutputArgument(pvCtx) > 3)
        {
            //4th Lhs
            double* pdblSize;
            sciErr = allocMatrixOfDouble(pvCtx, nbInputArgument(pvCtx) + 4, iNbItem, 1, &pdblSize);
            for (int i = 0 ; i < iNbItem ; i++)
            {
                pdblSize[i] = pInfo[i].iSize;
            }

            AssignOutputVariable(pvCtx, 4) = nbInputArgument(pvCtx) + 4;
        }

    }

    FREE(pInfo);
    ReturnArguments(pvCtx);
    return 0;
}