Esempio n. 1
0
///////////////////////////////////////////////////////////////////////////
// Tic-Tac-Toe                                             April 4, 2011 //
//                                                                       //
// Author: BJ Peter "Beej" DeLaCruz                                      //
//                                                                       //
// Description: A game of tic-tac-toe. Players take turns putting an X   //
//              or an O in a matrix. Whoever fills in all X's or O's in  //
//              a row, column, or diagonal first wins. Game ends when    //
//              there is a tie. Size of matrix is defined by ROW and COL //
//              in the constants.h header file. ROW and COL must be      //
//              equal to each other. The matrix can be any size, e.g.    //
//              10 x 10, so feel free to modify the ROW and COL          //
//              constants. Enjoy, and have fun!                          //
//                                                                       //
// Version: 1.1                                                          //
//                                                                       //
// Input parameters: None                                                //
//                                                                       //
// Returns: -1 if problems were encountered                              //
//           0 on success                                                //
//                                                                       //
///////////////////////////////////////////////////////////////////////////
int main(void) {
  char whoseTurn           = 'X';
  char didPlayerWin        = FALSE;
  char isTie               = FALSE;
  char** ppcTwoDArray      = NULL;
  TwoDArraySizes* pmsSizes = NULL;

  pmsSizes = (TwoDArraySizes*) malloc(sizeof(TwoDArraySizes));
  if (pmsSizes == NULL) {
    printf("Memory allocation failure for pmsSizes!\n");
    return -1;
  }

  pmsSizes->num_rows = MAX_ROW_SIZE;
  pmsSizes->num_cols = MAX_COL_SIZE;

  ppcTwoDArray = create2dArray(pmsSizes);
  if (ppcTwoDArray == NULL) {
    printf("Memory allocation failure for ppcTwoDArray!\n");
    return -1;
  }

  printf("\nTic-Tac-Toe\n");
  printf("----------------------\n");
  printf("How to play:\n");
  printf("   > Whoever gets all X's or all O's in a row, column, or diagonal first wins!\n\n");
  printf("%c goes first.\n\n", whoseTurn);

  while (!didPlayerWin && !isTie) {
    if (getPlayerMove(ppcTwoDArray, &whoseTurn, pmsSizes) < 0) {
      return -1;
    }

    if (checkRowsColumnsAndDiagonals(ppcTwoDArray, &whoseTurn, &didPlayerWin, &isTie, pmsSizes) < 0) {
      return -1;
    }

    if (print2dArray(ppcTwoDArray, pmsSizes) < 0) {
      return -1;
    }

    if (isTie || didPlayerWin) {
      break;
    }

    whoseTurn = (whoseTurn == 'X') ? 'O' : 'X';
  }

  if (didPlayerWin) {
    printf("%c won!\n\n", whoseTurn);
  }
  else {
    printf("The game is tie.\n\n");
  }

  free2dArray(ppcTwoDArray, pmsSizes);

  return 0;
}
Esempio n. 2
0
int
main(int argc, char **argv)
{
    double starttime, endtime;
    int ntasks, myrank;
    char name[128];                      
    int namelen;
    MPI_Status status;

    MPI_Init(&argc, &argv);
    MPI_Comm_rank(MPI_COMM_WORLD, &myrank);
    MPI_Comm_size(MPI_COMM_WORLD, &ntasks);

    MPI_Get_processor_name(name,&namelen);

    /* Get a few OpenMP parameters.                                               */
    int O_P  = omp_get_num_procs();          /* get number of OpenMP processors       */
    int O_T  = omp_get_num_threads();        /* get number of OpenMP threads          */
    int O_ID = omp_get_thread_num();         /* get OpenMP thread ID                  */
    //printf("name:%s   M_ID:%d  O_ID:%d  O_P:%d  O_T:%d\n", name,myrank,O_ID,O_P,O_T);

    FILE *f;
    char line[LINE_SIZE];
    int numlines = 0;

    exprinfo *exprannot = NULL;
    char **glines = NULL;

    f = fopen("gene_list.txt", "r");
    while(fgets(line, LINE_SIZE, f)) {
        glines = (char**)realloc(glines, sizeof(char*)*(numlines+1));
        glines[numlines] = strdup(line);

        char *pch = strtok (line,",");
        char * gene = pch;

        pch = strtok (NULL, ",");
        int chr = atoi(trimwhitespace(pch));

        exprannot = (exprinfo*)realloc(exprannot,sizeof(exprinfo)*(numlines+1));

        exprannot[numlines].gene      = strdup(gene);
        exprannot[numlines].chr       = chr;

        if (!exprannot) printf("not allcoated\n");
        numlines++;
    }
    fclose(f);
    f = fopen("probe_id_mapping.txt", "r");
    numlines = 0;

    free(glines[0]);
    free(glines);

    probeinfo *records = NULL;
    char **lines = NULL;

    while(fgets(line, LINE_SIZE, f)) {              
        lines = (char**)realloc(lines, sizeof(char*)*(numlines+1));
        lines[numlines] = strdup(line);

        char *pch = strtok (line,",");
        int probeid = atoi(pch);

        pch = strtok (NULL, ",");
        char * gene = pch;

        pch = strtok (NULL, ",");
        int chr = atoi(trimwhitespace(pch));

        records = (probeinfo*)realloc(records,sizeof(probeinfo)*(numlines+1));

        records[numlines].probeid   = probeid;
        records[numlines].chr       = chr;
        records[numlines].gene      = strdup(gene);
        if (!records) printf("not allcoated\n");
        numlines++;

    }
    free(lines[0]);
    free(lines);

    fclose(f);


    int NUM_GENES = numlines;
    unsigned long x_nr, x_nc, y_nr, y_nc;

    double **X = h5_read("x.h5", 1, "/X",         &x_nr, &x_nc);
    double **Y = h5_read("filtered_probes.h5", 1, "/FilteredProbes", &y_nr, &y_nc);

    //printf("loaded X, num rows = %d, num cols = %d\n", x_nr, x_nc);
    //printf("loaded Y, num rows = %d, num cols = %d\n", y_nr, y_nc);

    unsigned long total_mem = (x_nr * y_nc);
    double **RHO   = create2dArray(x_nr, y_nc);
    
    int gene, probe, tid, work_completed;
    work_completed = 0;

    
    int BLOCK_SIZE = NUM_ROWS/ntasks;
    int offset = myrank*BLOCK_SIZE;
    int STOP_IDX = offset+BLOCK_SIZE;
    if (NUM_ROWS - STOP_IDX < BLOCK_SIZE)
        STOP_IDX = NUM_ROWS;

   // printf("offset = %d, for rank %d, with ntasks = %d, and BLOCK_SIZE = %d, STOP_IDX = %d\n", 
    //            offset, myrank, ntasks, BLOCK_SIZE, STOP_IDX);

    int num_sig_found = 0; 
    starttime = MPI_Wtime();

    #pragma omp parallel \
     for shared(X, Y, RHO, BLOCK_SIZE, offset, work_completed) \
     private(probe,gene, tid)
    for (gene = offset; gene < STOP_IDX; gene++) {
       for (probe = 0; probe < NUM_COLS; probe++) {
           double *x = getrowvec(X, gene, BUFFER_SIZE);
           double *y = getcolvec(Y, probe, BUFFER_SIZE);

           double avgx = mean(x, BUFFER_SIZE);
           double * xcentered = sub(x, avgx, BUFFER_SIZE);
                    
           double avgy = mean(y, BUFFER_SIZE);
           double * ycentered = sub(y, avgy, BUFFER_SIZE);
           
           double * prod_result = prod(xcentered, ycentered, BUFFER_SIZE);
           double sum_prod = sum(prod_result, BUFFER_SIZE);

           double stdX = stddev(x, avgx, BUFFER_SIZE);
           double stdY = stddev(y, avgy, BUFFER_SIZE);
           double rho  = sum_prod/((BUFFER_SIZE-1)*(stdX*stdY));

           RHO[gene][probe] = rho;
           if (work_completed % 10000 == 0) {
               tid = omp_get_thread_num();
               printf("rank = %d, work = %d, result[%d,%d] from %d = %f\n", myrank, work_completed,
                    gene, probe, tid, rho);
           }
           work_completed++;
           free(x);
           free(y);
           free(xcentered);
           free(ycentered);
           free(prod_result);

       }
    }
    //printf("********* %d FINISHED **********\n", myrank);

    //f = fopen("significant.txt", "a");
    #pragma omp parallel for shared(RHO,exprannot, records)
    for (int i = 0; i < NUM_ROWS; i++) {
        for (int j = 0; j < NUM_COLS; j++) {
            double zscore = ztest(RHO[i][j],BUFFER_SIZE);
            /*
            if (zscore > 5.0) {

                fprintf(f, "%d,%d,%f,%f,%d,%s,%d,%s\n", 
                        i, j, zscore, RHO[i][j], records[j].chr, records[j].gene, exprannot[i].chr,exprannot[i].gene);
                if (i*j%1000 == 0)
                    printf("%d,%d,%f,%f,%d,%s,%d,%s\n", 
                        i, j, zscore, RHO[i][j], records[j].chr, records[j].gene, exprannot[i].chr,exprannot[i].gene);
            }
            */
        }
    }
    //fclose(f);
    endtime   = MPI_Wtime();
    free(X[0]);
    free(X);
    free(Y[0]);
    free(Y);
    printf("rank %d - elapse time - %f\n",myrank, endtime-starttime);
    //for (int i = 0; i < NUM_ROWS; i++) {
        //printf("%s,%d\n", exprannot[i].gene, exprannot[i].chr);
    //}
    //printf("rank %d FINISHED\n",myrank);
    //h5_write(RHO, NUM_ROWS, NUM_COLS, "rho_omp.h5", "/rho");
    free(RHO[0]);
    free(exprannot);
    free(records);
    free(RHO);

  MPI_Finalize();
  return 0;
}