Example #1
0
void *work_thread (void *lp) {
    int task_id = *((int *) lp);
    int begin, end;
    struct timeval start, finish;
    int i;
    /*get the divided task*/
    begin = (nsize * task_id) / task_num + 1;
    end = (nsize * (task_id + 1)) / task_num;
    if(task_id==0) gettimeofday (&start, NULL);
    fprintf (stderr, "thread %d: begin %d, end %d\n", task_id, begin, end);

    barrier (task_num);
    /* initialization */
    if (task_id == 0)
    initRHS(nsize, begin, end);
    barrier (task_num);
    initResult(nsize, begin, end);
    barrier (task_num);
    /* Gauss Compute */
    computeGauss(nsize, task_id);
    barrier (task_num);

    /* Gauss computation done */
    if(task_id==0) {
	/* Since there are dependencies in computing equation stage
    the upper part need the results of upper part), it should be done by thread 0 solely. */
        solveGauss(nsize);
        gettimeofday (&finish, NULL);
        printf ("Elapsed time: %.2f seconds\n",
	        (((finish.tv_sec * 1000000.0) + finish.tv_usec) -
	        ((start.tv_sec * 1000000.0) + start.tv_usec)) / 1000000.0);
    }
}
int main(int argc, char *argv[])
{
    int i;
    struct timeval start, finish;
    double error;
    
    pthread_attr_t attr;
    pthread_t *tid;
    int *id;
    
    if (argc < 2) {
	fprintf(stderr, "usage: %s <matrixfile>\n", argv[0]);
	exit(-1);
    }
    
    // for getting the threads
    if(argc == 3) {
        task_num = strtol(argv[2], NULL, 10);
    }

    nsize = initMatrix(argv[1]);
    initRHS(nsize);
    initResult(nsize);
   
    // create threads
    id = (int *) malloc (sizeof (int) * task_num);
    tid = (pthread_t *) malloc (sizeof (pthread_t) * task_num);
    if (!id || !tid)
        errexit ("out of shared memory");
    pthread_attr_init (&attr);
    pthread_attr_setscope (&attr, PTHREAD_SCOPE_SYSTEM);
    for (i = 1; i < task_num; i++) {
        id[i] = i;
        pthread_create (&tid[i], &attr, work_thread, &id[i]);
    }

    id[0]=0;
    work_thread(&id[0]);
    // wait for all threads to finish
    for (i = 1; i < task_num; i++)
        pthread_join (tid[i], NULL);
	    
    solveGauss(nsize);
    
    error = 0.0;
    for (i = 0; i < nsize; i++) {
	double error__ = (X__[i]==0.0) ? 1.0 : fabs((X[i]-X__[i])/X__[i]);
	if (error < error__) {
		error = error__;
	}
    }
    fprintf(stdout, "Error: %e\n", error);
}
int main(int argc,char *argv[])
{
    int i;
    pthread_t *tid;
    int *id;
    
    double mhz;
    hrtime_t hrcycle1, hrcycle2;

    while((i = getopt(argc,argv,"s:p:")) != -1){
        switch(i){
            case 's':
                {
                    int s;
                    s = atoi(optarg);
                    if (s > 0){
                        nsize = s;
                    } else {
                        fprintf(stderr,"Entered size is negative, hence using the default (%d)\n",(int)NSIZE);
                    }
                }
                break;
            case 'p':
                {
                    int p;
                    p = atoi(optarg);
                    if (p > 0){
                        task_num = p;
                    } else {
                        fprintf(stderr,"Entered task number is negative, hence using the default value: 1\n");
                    }
                }
                break;
            default:
                assert(0);
                break;
        }
    }
    
#ifdef CHECK_CORRECTNESS
    nsize = 3;
    allocate_memory(nsize);
    initCheck(nsize);
#else
    allocate_memory(nsize);
    initMatrix(nsize);
#endif
    
    hrcycle1 = gethrcycle_x86();
/* create thread*/
    id = (int *) malloc (sizeof (int) * task_num);
    tid = (pthread_t *) malloc (sizeof (pthread_t *) * task_num);
    if (!id || !tid)
        errexit ("out of shared memory");


    for(i=1; i<task_num; i++){
        id[i] = i;
        pthread_create(&tid[i], NULL, work_thread, &id[i]);
    }

    id[0] = 0;
    work_thread(&id[0]);

    for(i=1; i<task_num; i++){
        pthread_join(tid[i], NULL);
    }

#if VERIFY
    solveGauss(nsize);
#endif

    hrcycle2 = gethrcycle_x86();
    mhz = getMHZ_x86();

    printf("Here hrcycle = %lld, mhz = %f HZ\n", hrcycle2 - hrcycle1, mhz);


#if VERIFY
    for(i = 0; i < nsize; i++)
        printf("%6.5f %5.5f\n",B[i],C[i]);
#endif
    
    return 0;
}
Example #4
0
int main(int argc, char *argv[])
{
    int i;
    struct timeval start, finish;
    double error;

    pthread_attr_t attr;
    pthread_t *tid;
    int *id;
    
    //Ensure that the program takes two parameters
    //  first param is the input matrix and the second is the number of processors for the parallel run    
    if (argc != 3) {
        fprintf(stderr, "usage: %s <matrixfile> <#ofProcesses>\n", argv[0]);
        exit(-1);
    }

    task_num = atoi(argv[2]);
    nsize = initMatrix(argv[1]);
    initRHS(nsize);
    initResult(nsize);

    gettimeofday(&start, 0);

    // create threads
    id = (int *) malloc (sizeof (int) * task_num);
    tid = (pthread_t *) malloc (sizeof (pthread_t) * task_num);
    if (!id || !tid)
        errexit ("out of shared memory");
    pthread_attr_init (&attr);
    pthread_attr_setscope (&attr, PTHREAD_SCOPE_SYSTEM);
    for (i = 1; i < task_num; i++) {
        id[i] = i;
        pthread_create (&tid[i], &attr, work_thread, &id[i]);
    }

    id[0]=0;
    work_thread(&id[0]);
    // wait for all threads to finish
    for (i = 1; i < task_num; i++)
        pthread_join (tid[i], NULL);

    gettimeofday(&finish, 0);

    solveGauss(nsize);

    fprintf(stdout, "Time:  %f seconds\n", (finish.tv_sec - start.tv_sec) + (finish.tv_usec - start.tv_usec)*0.000001);

    
    error = 0.0;
    for (i = 0; i < nsize; i++) {
    double error__ = (X__[i]==0.0) ? 1.0 : fabs((X[i]-X__[i])/X__[i]);
    if (error < error__) {
        error = error__;
    }
    }
    fprintf(stdout, "Error: %e\n", error);


    //File output
    // float timeOutput = ((finish.tv_sec - start.tv_sec) + (finish.tv_usec - start.tv_usec)*0.000001);

    // FILE *pthreadTimingFile = fopen("pthread1Timing.txt", "a");
    // fprintf(pthreadTimingFile, "%s %d %f %G\n", argv[1], task_num, timeOutput, error);
    // fclose(pthreadTimingFile);

    return 0;
}