Ejemplo n.º 1
0
/*
 * The mex function runs a betweenness-centrality problem.
 */
void mexFunction(int nlhs, mxArray *plhs[],
                 int nrhs, const mxArray *prhs[])
{   
    mwIndex mrows, ncols;
    
    mwIndex n,nz;
    
    /* sparse matrix */
    mwIndex *ia, *ja;
    double *a;
    
    /* output data */
    double *bc;
    double *ec;
    
    /* used to switch between algorithm types */
    int unweighted;
     
    if (nrhs != 2) 
    {
        mexErrMsgTxt("2 inputs required.");
    }
    
    /* The second input must be a scalar. */
    if (mxGetNumberOfElements(prhs[1]) > 1 || !mxIsDouble(prhs[1]))
    {
        mexErrMsgTxt("Invalid scalar.");
    }
    
    /* get the scalar, because it affects what type of matrices we can look at */
    unweighted = (int)mxGetScalar(prhs[1]);

    /* The first input must be a sparse matrix. */
    mrows = mxGetM(prhs[0]);
    ncols = mxGetN(prhs[0]);
    if (mrows != ncols ||
        !mxIsSparse(prhs[0]) ||
        ((!mxIsDouble(prhs[0]) || mxIsComplex(prhs[0])) && !unweighted)
        )
    {
        mexErrMsgTxt("Input must be a square sparse matrix.");
    }
    
    n = mrows;
         
    
    /* Get the sparse matrix */
    
    /* recall that we've transposed the matrix */
    ja = mxGetIr(prhs[0]);
    ia = mxGetJc(prhs[0]);
    
    nz = ia[n];
    
    
    
    plhs[0] = mxCreateDoubleMatrix(n,1,mxREAL);
    /* create the output vectors */    
    bc = mxGetPr(plhs[0]);

    /* if they requested edge centrality, compute that as well */
    if (nlhs > 1) {
        plhs[1] = mxCreateDoubleMatrix(nz,1,mxREAL);
        ec = mxGetPr(plhs[1]);
    } else {
        ec = NULL;
    }
    
    
    #ifdef _DEBUG
    mexPrintf("betweenness_centrality...");
    #endif 
    
    if (unweighted)
    {
        betweenness_centrality(n, ja, ia, NULL,
            bc, ec);
    }
    else
    {
        a = mxGetPr(prhs[0]);
        betweenness_centrality(n, ja, ia, a,
            bc, ec);
    }
    #ifdef _DEBUG
    mexPrintf("done\n");
    #endif 
    
    #ifdef _DEBUG
    mexPrintf("return\n");
    #endif 
}
Ejemplo n.º 2
0
Archivo: main.c Proyecto: hvarg/Memoria
int main(int argc, const char * args[])
{
  if (argc < 2) {
    printf("Modo de uso: ./centrality archivo.sg [#threads]\n");
    printf("Si #threads es menor a 1 no se calculara la intermediacion.\n");
    return EXIT_SUCCESS;
  }
  char  *filename = (char*) args[1]; 
  printf("File: %s\n", filename);
  int   NT = (argc > 2) ? atoi(args[2]) : 4,
        i, j, gap;
  float *BC;
  struct data  **D;
  struct p_th  *P;

  if ( (G = file_to_graph(filename)) == NULL) 
    return EXIT_FAILURE;
  if (NT > 1) {
    printf("Threads: %d\n", NT);
    gap = (G->size%NT == 0) ? G->size/NT : (G->size/NT)+1;
    BC  = (float*) malloc(sizeof(float) * G->size);
    D   = (struct data**) malloc(sizeof(struct data*)*NT);
    P   = pth_create(NT);

    for (i = 0; i < NT; i++) {
      D[i] = (struct data*) malloc(sizeof(struct data));
      D[i]->init = i*gap;
      D[i]->end  = (i+1)*gap;
      D[i]->bc   = (float*) malloc(sizeof(float) * G->size);
    }

    for (i = 0; i < NT; i++) {
      pth_send_job(P, __cent, D[i]);
    }
    pth_wait(P);

    for (i = 0; i < G->size; i++) { //se puede acumular todo en el primero.
      BC[i] = 0.0;
      for (j = 0; j < NT; j++) {
        BC[i] += D[j]->bc[i];
      }
    }
    pth_del(P);
    for (i = 0; i < NT; i++) {
      free(D[i]->bc);
      free(D[i]);
    }
    free(D);
  } else if (NT == 1) {
    printf("Threads: Single core\n");
    BC = betweenness_centrality(G);
  } else {
    printf("Calculando solo centralidad de grado.\n");
    BC = (float *) calloc(G->size, sizeof(float));
  }

  /* Guardando los resultados en archivos. */
  short fn_size = strlen(filename);
  char *bc_out  = (char *) malloc(sizeof(char) * (fn_size + 11)),
       *idc_out = (char *) malloc(sizeof(char) * (fn_size + 12)),
       *odc_out = (char *) malloc(sizeof(char) * (fn_size + 12));
  strcpy(bc_out, filename);
  strcpy(idc_out, filename);
  strcpy(odc_out, filename);
  strcat(bc_out, ".bc.result");
  strcat(idc_out, ".idc.result");
  strcat(odc_out, ".odc.result");
  FILE *fbc = fopen(bc_out, "w"),
       *fidc = fopen(idc_out, "w"),
       *fodc = fopen(odc_out, "w");
  for (i=0; i< G->size; i++){
    fprintf(fbc,"%d: %f\n", i, BC[i]);
    fprintf(fidc,"%d: %d\n", i, IDC[i]);
    fprintf(fodc,"%d: %d\n", i, ODC[i]);
  }
  fclose(fbc);
  fclose(fidc);
  fclose(fodc);
  free(bc_out);
  free(idc_out);
  free(odc_out);

  graph_del(G);
  free(BC);
  free(IDC);
  free(ODC);
  return EXIT_SUCCESS;
}