Example #1
0
int main(int argc, char **argv) {
    int *array;
    int *subarray;
    int arraysize = 0;
    int subarrsize = 0;
    int seed;
    int processorsize=0;
    char randomstate[8];
    struct results subr;
    char file[processorsize];
    int i;
    int status;
    pid_t pid;
    FILE *fw;
    FILE *fr;


    /* process command line arguments */
    if (argc != 4) {
        printf("usage: ./findminmax <seed> <arraysize> <nprocs>\n");
        return 1;
    }

    seed = atoi(argv[1]);
    arraysize = atoi(argv[2]);
    processorsize = atoi(argv[3]);
    subarrsize = arraysize / processorsize;

    /* allocate array and populate with random values */
    array = (int *) malloc(sizeof(int) * arraysize);
    subarray = (int *) malloc(sizeof(int) * subarrsize);

    initstate(seed, randomstate, 8);

    for (i = 0; i < arraysize; i++) {
        array[i] = random();
    }


    /* parallel Min and Max Verison 1 (results communicated via files) */
    mtf_measure_begin();
    for ( i = 0; i < processorsize; i++) {
        pid = fork();
        if (pid == -1) {
            printf("Error in generating processors!");
            exit(1);
        }
        else if (pid == 0) {
            subarray = array + i*subarrsize;
            subr = find_min_and_max(subarray, subarrsize);

            sprintf(file,"text%d.txt",i);
            fw = fopen(file, "w");
            if (fw == NULL) {
                printf("Error opening file!\n");
                return 1;
            }
            fprintf(fw, "%d\n%d", subr.min, subr.max);
            fclose(fw);
            exit(0);
        }
    }

    for (i = 0; i < processorsize; i++) {
        wait(&status);
    }

    int tmpmin,tmpmax, min, max;
    for (i = 0; i < processorsize; i++) {
        sprintf(file,"text%d.txt",i);
        fr = fopen(file, "r");
        if (fr == NULL) {
            printf("Error in opening file!\n");
            return 1;
        }

        while (!feof(fr)) {
            fscanf(fr, "%d\n%d", &tmpmin, &tmpmax);
            if (i == 0 ) {
                min = tmpmin;
                max = tmpmax;
            }
            else {
                if (tmpmin < min) {
                    min = tmpmin;
                }
                else if (tmpmax > max) {
                    max = tmpmax;
                }
            }
        }
        fclose(fr);
        remove(file);
    }


    free(array);
    mtf_measure_end();

    printf("Execution time: ");
    mtf_measure_print_seconds(1);

    printf("min = %d, max = %d\n", min, max);

    return 0;
}
Example #2
0
int main(int argc, char **argv)
{
    int *array;
    int arraysize = 0;
    int seed;
    char randomstate[8];
    struct results r;
    int i, j, fd, x, y;
    int nprocs;
    int chunk;

    
    /* process command line arguments */
    if (argc != 4) {
        printf("usage: ./findminmax <seed> <arraysize> <nprocs>\n");
        return 1;
    }
    
    seed = atoi(argv[1]);
    arraysize = atoi(argv[2]);
    nprocs = atoi(argv[3]);
    
    /* allocate array and populate with random values */
    chunk = arraysize / nprocs;
    array = (int *) malloc(sizeof(int) * arraysize);
 
    
    initstate(seed, randomstate, 8);
    
    
    for (i = 0; i < arraysize; i++) {
        array[i] = random();
    }
    
    /* begin computation */
    
    mtf_measure_begin();
    
    int pid;

    for(j = 0; j < nprocs; j++) {
        pid = fork();
        
        char *file;
        file = malloc(sizeof(char) * 1);
        
        sprintf(file, "%d", j);
        
        if ( (fd = open(file, O_CREAT | O_WRONLY, 0600)) < 0) {
            printf("Cannot open %s\n", file);
            exit(1);
        }
        
        
        if(pid < 0) {
            printf("Error in making child processes.");
            exit(0);
        } else if(pid == 0) { 
            r = find_min_and_max(array, chunk);
            write(fd, &r, sizeof(struct results));
            close(fd);
            exit(0);
                
            
        } else {
            array = array + chunk;
        
    }
        
    for(x = 0; x < nprocs; x++) {
        pid = wait(NULL);
    }
       
    for(y = 0; y < nprocs; y++) {
        if ((fd = open(file, O_RDONLY)) < 0) {
            printf("Cannot open %s\n", file);
            exit(1);
        }
        
        struct results vals;
        read(fd, &vals, sizeof(struct results));
        
        if (vals.min <= r.min){
            r.min = vals.min;
        }
    
        if (vals.max >= r.max){
            r.max = vals.max;
        }
    
        }
    
  }
    
    
    
    mtf_measure_end();
    
    printf("Execution time: ");
    mtf_measure_print_seconds(1);
    
    printf("min = %d, max = %d\n", r.min, r.max);
    close(fd);
    return 0;
}
Example #3
0
/**
 * The main() function accepts two command-line arguments: a seed value and the size of the random array to generate.
 *  The seed value is used by the random number generator.  If you want to test with the same randomly generated array,
 *  you should use the same seed value from run to run.  To test different arrays, use different seed values.
 *   This will be useful when testing your parallel versions for correctness.
 *   For example you can invoke findminmax_seq as follows:
 *
 *   $ findminmax_seq 1 10000 1

This will use a seed value of 1 and generate an array of size 10000 with 1 process.
 *
 */
int main(int argc, char **argv)
{
    int *array;
    int arraysize = 0;
    int seed;
    int nprocs=1;

    /**
     * How much each process sorts and how much extra is left over that we should give to the last process
     */
    int *pipes;
    pid_t *sub_proc;
    int proc_share=0;
    int proc_leftover=0;
   	int offset=0;
   	int launched=0;
   	int * proc_results;

    char randomstate[8];
    struct results r;
    int i;

    /* process command line arguments */
    if (argc != 4) {
        printf("usage: ./findminmax <seed> <arraysize> <nprocs>\n");
        return 1;
    }

    seed = atoi(argv[1]);
    arraysize = atoi(argv[2]);
    nprocs = atoi(argv[3]);

    proc_share=arraysize/nprocs;
    proc_leftover=arraysize%nprocs;
    proc_results = (int *) malloc(sizeof(int) * nprocs*2);

    /* allocate array and populate with random values */
    array = (int *) malloc(sizeof(int) * arraysize);
    
    initstate(seed, randomstate, 8);

    for (i = 0; i < arraysize; i++) {
        array[i] = random();
    }
    
    /* begin computation */

    pipes=(int *) malloc(sizeof(int)*nprocs*2);
    sub_proc=(pid_t *) malloc(sizeof(pid_t)*nprocs);

   	mtf_measure_begin();
   	for(i=0 ; i < nprocs;i++) {
   		pipe(&pipes[i*2]);
   		int size=proc_share+(proc_leftover > i ? 1 : 0);
   		pid_t tmp_pid=fork();
   		if (!tmp_pid)  {
   		    int write_pipe=pipes[i*2+1];

   		    FILE *pipe=fdopen(write_pipe,"wb");
   			r = find_min_and_max(&array[offset], size);
   		    printf("min = %d, max = %d size->%d\n", r.min, r.max,size);
   			fwrite(&r,sizeof(struct results),1,pipe);
   			fclose(pipe);
   			return 0;
   		} else {
   			launched++;
   			sub_proc[i]=tmp_pid;
   		}
   	    offset+=size;
    }
   	while(launched>0) {
   		for(i=0 ; i < nprocs;i++) {
   			struct results tmp_result;
   			int status=0;
   			int pid=0;
   			if (sub_proc[i]==0) { continue; }
   			pid=waitpid(sub_proc[i],&status,0);
   			if (pid) {
   	   		    int read_pipe=pipes[i*2];
   	   		    FILE *pipe=fdopen(read_pipe,"rb");
   				launched--;
   				sub_proc[i]=0;
   	   			fread(&tmp_result,sizeof(struct results),1,pipe);
   	   		    printf("parent -> min = %d, max = %d\n", tmp_result.min, tmp_result.max);
   	   			fclose(pipe);
   	   			proc_results[i*2]=tmp_result.max;
   	   			proc_results[i*2+1]=tmp_result.min;
   			}
   		}
   	}
	r = find_min_and_max(proc_results, nprocs*2);


    mtf_measure_end();
    
    printf("Execution time: ");
    mtf_measure_print_seconds(1);

    printf("min = %d, max = %d\n", r.min, r.max);

	r = find_min_and_max(array, arraysize);

	printf("check -> min = %d, max = %d\n", r.min, r.max);

    return 0;
}