Beispiel #1
0
int main(int argc, char **argv)
{
    int i, j, imax, jmax;
    int outmode = ZETA, verbose = 1;
    float xlength, ylength;
    float zmax = -1e10, zmin = 1e10;
    float pmax = -1e10, pmin = 1e10;
    float umax = -1e10, umin = 1e10;
    float vmax = -1e10, vmin = 1e10;

    int show_help = 0, show_usage = 0, show_version = 0;
    char *infile = NULL, *outfile = NULL;
    FILE *fin = stdin, *fout = stdout;

    progname = argv[0];
    int optc;
    while ((optc = getopt_long(argc, argv, GETOPTS, long_opts, NULL)) != -1) {
        switch (optc) {
            case 'h':
                show_help = 1;
                break;
            case 'V':
                show_version = 1;
                break;
            case 'v':
                verbose = atoi(optarg);
                break;
            case 'i':
                if (infile != NULL) {
                    free(infile);
                }
                infile = strdup(optarg);
                break;
            case 'o':
                if (outfile != NULL) {
                    free(outfile);
                }
                outfile = strdup(optarg);
                break;
            default:
                show_usage = 1;
        }
    }

    if (show_usage || optind < argc) {
        print_usage();
        return 1;
    }

    if (show_version) {
        print_version();
        if (!show_help) {
            return 0;
        }
    }

    if (show_help) {
        print_help();
        return 0;
    }

    if (infile != NULL) {
        fin = fopen(infile, "rb");
        if (!fin) {
            fprintf(stderr, "Could not open '%s'\n", infile);
            return 1;
        }
    }

    if (outfile != NULL) {
        fout = fopen(outfile, "wb");
        if (!fout) {
            fprintf(stderr, "Could not open '%s'\n", outfile);
            return 1;
        }
    }
    fread(&imax, sizeof(int), 1, fin);
    fread(&jmax, sizeof(int), 1, fin);

    float **u    = alloc_floatmatrix(imax+2, jmax+2);
    float **v    = alloc_floatmatrix(imax+2, jmax+2);
    float **p    = alloc_floatmatrix(imax+2, jmax+2);
    float **psi  = alloc_floatmatrix(imax+2, jmax+2);
    float **zeta = alloc_floatmatrix(imax+2, jmax+2);
    char  **flag = alloc_charmatrix(imax+2, jmax+2);

    if (!u || !v || !p || !psi || !zeta || !flag) {
        fprintf(stderr, "Couldn't allocate memory for matrices.\n");
        return 1;
    }

    fread(&xlength, sizeof(float), 1, fin);
    fread(&ylength, sizeof(float), 1, fin);
    float delx = xlength/imax;
    float dely = ylength/jmax;

    if (verbose > 1) {
        printf("imax: %d\n", imax);
        printf("jmax: %d\n", jmax);
        printf("xlength: %g\n", xlength);
        printf("ylength: %g\n", ylength);
    }
    for (i = 0; i <= imax+2; i++) {
        fread(u[i], sizeof(float), jmax+2, fin);
        fread(v[i], sizeof(float), jmax+2, fin);
        fread(p[i], sizeof(float), jmax+2, fin);
        fread(flag[i], sizeof(char), jmax+2, fin);
    }

    calc_psi_zeta(u, v, psi, zeta, flag, imax, jmax, delx, dely);
    fprintf(fout, "P6 %d %d 255\n", imax, jmax);

    for (j = 1; j < jmax+1 ; j++) {
        for (i = 1; i < imax+1 ; i++) {
            int r, g, b;
            if (!(flag[i][j] & C_F)) {
                r = 0; b = 0; g = 255;
            } else {
                zmax = max(zmax, zeta[i][j]);
                zmin = min(zmin, zeta[i][j]);
                pmax = max(pmax, psi[i][j]);
                pmin = min(pmin, psi[i][j]);
                umax = max(umax, u[i][j]);
                umin = min(umin, u[i][j]);
                vmax = max(vmax, v[i][j]);
                vmin = min(vmin, v[i][j]);
                if (outmode == ZETA) {
                    float z = (i < imax && j < jmax)?zeta[i][j]:0.0;
                    r = g = b = pow(fabs(z/12.6),.4) * 255;
                } else if (outmode == PSI) {
                    float p = (i < imax && j < jmax)?psi[i][j]:0.0;
                    r = g = b = (p+3.0)/7.5 * 255; 
                }
            }
            fprintf(fout, "%c%c%c", r, g, b);
        }
    }
    if (verbose > 0) {
        printf("u:    % .5e -- % .5e\n", umin, umax);
        printf("v:    % .5e -- % .5e\n", vmin, vmax);
        printf("psi:  % .5e -- % .5e\n", pmin, pmax);
        printf("zeta: % .5e -- % .5e\n", zmin, zmax);
    }
    fclose(fin);
    fclose(fout);

    free_matrix(u);
    free_matrix(v);
    free_matrix(p);
    free_matrix(psi);
    free_matrix(zeta);
    free_matrix(flag);

    return 0;
}
Beispiel #2
0
int main(int argc, char **argv)
{
    double t;
    int i, j, n, p;

    MPI_Init(&argc, &argv);
    MPI_Comm_size(MPI_COMM_WORLD, &n);
    MPI_Comm_rank(MPI_COMM_WORLD, &p);

    if (n != 2) {
        printf("Must be run on exactly 2 processors.\n");
        MPI_Finalize();
        return 1;
    }

    t = MPI_Wtime();

    /* Allocate an COLS * ROWS array. */
    float **matrix;
    matrix = alloc_floatmatrix(COLS, ROWS);

    /* Fill processor 1's matrix with numbers */
    for (i = 0; i < COLS; i++) {
        for ( j = 0; j < ROWS; j++) {
            matrix[i][j] = (i * 10) + j;
        }
    }

    /* Define two MPI_Datatypes for rows that we use later */
    MPI_Datatype fullrowtype, partrowtype;
    MPI_Type_vector(ROWS, 1, ROWS, MPI_FLOAT, &fullrowtype);
    MPI_Type_commit(&fullrowtype);
    MPI_Type_vector(3, 1, ROWS, MPI_FLOAT, &partrowtype);
    MPI_Type_commit(&partrowtype);

    if (p == 0) {
        MPI_Status s;

        zeromatrix(matrix);
        MPI_Recv(matrix[4], ROWS, MPI_FLOAT, 1, 0, MPI_COMM_WORLD, &s);
        printmatrix("After receiving column 4:", matrix);

        zeromatrix(matrix);
        MPI_Recv(&matrix[6][2], 4, MPI_FLOAT, 1, 0, MPI_COMM_WORLD, &s);
        printmatrix("After receiving column 6, rows 3-5:", matrix);

        zeromatrix(matrix);
        MPI_Recv(matrix[3], ROWS*2, MPI_FLOAT, 1, 0, MPI_COMM_WORLD, &s);
        printmatrix("After receiving column 3 and 4:", matrix);

        zeromatrix(matrix);
        MPI_Recv(matrix[0], ROWS*COLS, MPI_FLOAT, 1, 0, MPI_COMM_WORLD, &s);
        printmatrix("After receiving all columns:", matrix);

        zeromatrix(matrix);
        MPI_Recv(&matrix[0][6], 1, fullrowtype, 1, 0, MPI_COMM_WORLD, &s);
        printmatrix("After receiving row 6:", matrix);
       
        zeromatrix(matrix); 
        MPI_Recv(&matrix[0][1], 1, partrowtype, 1, 0, MPI_COMM_WORLD, &s);
        printmatrix("After receiving row 1 cols 0-2:", matrix);
        
        zeromatrix(matrix); 
        MPI_Recv(&matrix[4][1], 1, partrowtype, 1, 0, MPI_COMM_WORLD, &s);
        printmatrix("After receiving row 1 cols 4-6:", matrix);
    } else {
        /* Send all of column 4 to processor 0 */
        MPI_Send(matrix[4], ROWS, MPI_FLOAT, 0, 0, MPI_COMM_WORLD);

        /* Send column 6 rows 2-5 to processor 0 */
        MPI_Send(&matrix[6][2], 4, MPI_FLOAT, 0, 0, MPI_COMM_WORLD);

        /* Send columns 3 and 4 to processor 0 */
        MPI_Send(matrix[3], ROWS*2, MPI_FLOAT, 0, 0, MPI_COMM_WORLD);

        /* Send the entire matrix (ie all columns) to processor 0 */
        MPI_Send(matrix[0], ROWS*COLS, MPI_FLOAT, 0, 0, MPI_COMM_WORLD);

        /* Send row 6 to processor 0 */
        MPI_Send(&matrix[0][6], 1, fullrowtype, 0, 0, MPI_COMM_WORLD);
        
        /* Send row 1 cols 0-2 to processor 0 */
        MPI_Send(&matrix[0][1], 1, partrowtype, 0, 0, MPI_COMM_WORLD);
        
        /* Send row 1 cols 4-6 to processor 0 */
        MPI_Send(&matrix[4][1], 1, partrowtype, 0, 0, MPI_COMM_WORLD);
    }
    if (p == 0) {
        t = MPI_Wtime() - t;
        printf("Program took %f secs to run.\n", t);
    }

    /* Free the matrix we allocated */
    free_matrix(matrix);

    /* Free the derived MPI_Datatypes */
    MPI_Type_free(&fullrowtype);
    MPI_Type_free(&partrowtype);

    MPI_Finalize();
    return 0;
}