Ejemplo n.º 1
0
int main(void)
{
  int res;
  printf("========= Starts SBM tests 4 for SBM ========= \n");
  SparseBlockStructuredMatrix M;
  FILE *file = fopen("data/SBM2.dat", "r");
  newFromFileSBM(&M, file);
  printSBM(&M);
  fclose(file);
  /*alloc enough memory */
  CSparseMatrix sparseMat;
  res = SBMtoSparseInitMemory(&M, &sparseMat);
  if (res)
  {
    printf("========= Failed SBM tests 4 for SBM  ========= \n");
    return 1;
  }

  res = SBMtoSparse(&M, &sparseMat);
  if (res)
  {
    printf("========= Failed SBM tests 4 for SBM  ========= \n");
    return 1;
  }
  cs_print(&sparseMat, 1);
  cs_spfree_on_stack(&sparseMat);

  int n = M.blocksize0[M.blocknumber0 - 1];
  int m = M.blocksize1[M.blocknumber1 - 1];
  double * denseMat = (double *)malloc(n * m * sizeof(double));
  SBMtoDense(&M, denseMat);
  if (res)
  {
    printf("========= Failed SBM tests 4 for SBM  ========= \n");
    return 1;
  }
  printf("[");
  for (int i = 0; i < n * m; i++)
  {
    printf("%lf ", denseMat[i]);
    if ((i + 1) % m == 0)
      printf("\n");
  }
  printf("]");
  printf("\n (warning: column-major) \n");

  free(denseMat);
  printf("NUMERICS_SBM_FREE_BLOCK value %d", NUMERICS_SBM_FREE_BLOCK);
  SBMfree(&M, NUMERICS_SBM_FREE_BLOCK);
  printf("\n========= Succed SBM tests 4 for SBM  ========= \n");
  return 0;

}
Ejemplo n.º 2
0
int test_RowPermutationSBM(SparseBlockStructuredMatrix *M)
{
  SparseBlockStructuredMatrix MRes;
  unsigned int nbRow = M->blocknumber0;
  unsigned int * rowBlockIndex = (unsigned int*) malloc(nbRow * sizeof(unsigned int));
  unsigned int * mark = (unsigned int*) malloc(nbRow * sizeof(unsigned int));
  for (unsigned int i = 0; i < nbRow; i++)
  {
    mark[i] = 0;
  }
  for (unsigned int i = 0; i < nbRow; i++)
  {
    int candidate = rand() % nbRow;
    while (mark[candidate])
      candidate = rand() % nbRow;
    rowBlockIndex[i] = candidate;
    mark[candidate] = 1;
  }
  RowPermutationSBM(rowBlockIndex, M, &MRes);
  double * denseMRes = (double*) malloc(M->blocksize0[M->blocknumber0 - 1] * M->blocksize1[M->blocknumber1 - 1] * sizeof(double));
  SBMtoDense(&MRes, denseMRes);
  double * denseM = (double*) malloc(M->blocksize0[M->blocknumber0 - 1] * M->blocksize1[M->blocknumber1 - 1] * sizeof(double));
  unsigned int curRow = 0;
  unsigned int nbRowInM = M->blocksize0[M->blocknumber0 - 1];
  for (unsigned int i = 0; i < nbRow; i++)
  {
    unsigned int rowInM = rowBlockIndex[i];
    unsigned int nbRow = 0;
    if (rowInM)
      nbRow = M->blocksize0[rowInM] - M->blocksize0[rowInM - 1];
    else
      nbRow = M->blocksize0[rowInM];
    SBMRowToDense(M, rowInM, denseM, curRow, nbRowInM);
    curRow += nbRow;
  }
  for (unsigned int n = 0; n < M->blocksize0[M->blocknumber0 - 1]*M->blocksize1[M->blocknumber1 - 1]; n++)
    if (fabs(denseMRes[n] - denseM[n]) > 10e-12)
    {
      free(denseM);
      free(denseMRes);
      free(rowBlockIndex);
      free(mark);
      SBMfree(&MRes, 0);
      return 1;
    }
  free(denseM);
  free(denseMRes);
  free(rowBlockIndex);
  free(mark);
  SBMfree(&MRes, 0);
  return 0;
}
Ejemplo n.º 3
0
int test_SBMRowToDense(SparseBlockStructuredMatrix *M)
{
  double * denseRes = (double*) malloc(M->blocksize0[M->blocknumber0 - 1] * M->blocksize1[M->blocknumber1 - 1] * sizeof(double));
  unsigned int curRow = 0;
  unsigned int nbCol = M->blocksize1[M->blocknumber1 - 1];
  for (unsigned int i = 0; i < M->blocknumber0; i++)
  {
    unsigned int lLin = 0;
    unsigned int nbBlockRow = M->blocksize0[i] - curRow;
    SBMRowToDense(M, i, denseRes, 0, nbBlockRow);
    for (unsigned int lin = curRow; lin < M->blocksize0[i]; lin++)
    {
      unsigned int lCol = 0;
      for (unsigned int col = 0; col < nbCol; col++)
      {
        if (fabs(getValueSBM(M, lin, col) - denseRes[lLin + lCol * (nbBlockRow)]) > 10e-12)
        {
          free(denseRes);
          return 1;
        }
        lCol++;
      }
      lLin++;
    }
    curRow = M->blocksize0[i];
  }
  curRow = 0;
  for (unsigned int i = 0; i < M->blocknumber0; i++)
  {

    //    int lLin=0;
    //    int nbBlockRow=M->blocksize0[i]-curRow;
    SBMRowToDense(M, i, denseRes, curRow, M->blocksize0[M->blocknumber0 - 1]);
    curRow = M->blocksize0[i];
  }

  double * denseRes2 = (double*) malloc(M->blocksize0[M->blocknumber0 - 1] * M->blocksize1[M->blocknumber1 - 1] * sizeof(double));

  SBMtoDense(M, denseRes2);
  for (unsigned int n = 0; n < M->blocksize0[M->blocknumber0 - 1]*M->blocksize1[M->blocknumber1 - 1]; n++)
    if (fabs(denseRes2[n] - denseRes[n]) > 10e-12)
    {
      free(denseRes);
      free(denseRes2);
      return 1;
    }

  free(denseRes);
  free(denseRes2);
  return 0;
}
Ejemplo n.º 4
0
void _GMPReducedEquality(GenericMechanicalProblem* pInProblem, double * reducedProb, double * Qreduced, int * Me_size, int* Mi_size)
{

  SparseBlockStructuredMatrix* m = pInProblem->M->matrix1;
  int nbRow = m->blocksize0[m->blocknumber0 - 1];
  int nbCol = m->blocksize1[m->blocknumber1 - 1];

  _GMPReducedGetSizes(pInProblem, Me_size, Mi_size);
  if (*Me_size == 0)
  {
    memcpy(Qreduced, pInProblem->q, (*Mi_size)*sizeof(double));
    SBMtoDense(m, reducedProb);
    return;
  }

  double *Me = (*Me_size) ? (double *) malloc((*Me_size) * nbCol * sizeof(double)) : 0;
  double *Mi = (*Mi_size) ? (double *) malloc((*Mi_size) * nbCol * sizeof(double)) : 0;
  double *Qi = (double *) malloc(nbRow * sizeof(double));
  buildReducedGMP(pInProblem, Me, Mi, Qreduced, Qi, Me_size, Mi_size);

#ifdef GMP_DEBUG_GMPREDUCED_SOLVE
  double *Me1 = Me;
  double *Me2 = Me + (*Me_size) * (*Me_size);
  double *Mi1 = Mi;
  double *Mi2 = Mi + (*Mi_size) * (*Me_size);
  FILE * titi  = fopen("buildReduced2GMP_output.txt", "w");
  printf("GMP2Reducedsolve\n");
  printDenseMatrice("Me1", titi, Me1, *Me_size, *Me_size);
  printDenseMatrice("Me2", titi, Me2, *Me_size, *Mi_size);
  printDenseMatrice("Mi1", titi, Mi1, *Mi_size, *Me_size);
  printDenseMatrice("Mi2", titi, Mi2, *Mi_size, *Mi_size);
  printDenseMatrice("Qe", titi, Qreduced, *Me_size, 1);
  printDenseMatrice("Qi", titi, Qi, *Mi_size, 1);
  fclose(titi);
#endif
  for (int numCol = 0; numCol < nbCol; numCol++)
  {
    if (*Me_size)
      memcpy(reducedProb + numCol * nbRow, Me + numCol * (*Me_size), (*Me_size)*sizeof(double));
    if (*Mi_size)
      memcpy(reducedProb + numCol * nbRow + (*Me_size), Mi + numCol * (*Mi_size), (*Mi_size)*sizeof(double));
  }
  if (*Mi_size)
    memcpy(Qreduced + (*Me_size), Qi, (*Mi_size)*sizeof(double));
  free(Me);
  free(Mi);
  free(Qi);
}