WCM - Das österreichische Computer Magazin Forenübersicht
 

Zurück   WCM Forum > Meinung & Community > News & Branchengeflüster

News & Branchengeflüster aktuelle News von Lesern, sowie Tratsch und Insiderinformationen aus der Computerbranche

Microsoft KARRIERECAMPUS

 
 
Themen-Optionen Ansicht
Alt 31.08.2007, 19:11   #2
kikakater
Inventar
 
Registriert seit: 24.01.2001
Beiträge: 5.631


Standard

Microwulf: A Personal, Portable Beowulf Cluster



MPI Homework Project 1: Getting Started With MPI

Threads Homework Project 1: Threads in C

Code:
/* pi.c - parallel C code to demonstrate Linux thread interface 
 * Original Source: www.tldp.org/HOWTP/Parallel_processing-HOWTO 
 * Since PI == 4 * arctan(1), and arctan(x) is the 
 *  integral from 0 to x of (1/(1+x*x),
 *  the for loop below approximates that integration.
 * Generalized by: Joel Adams
 * Usage: ./a.out <numIntervals> <numThreads>
 */

#include <stdio.h>
#include <stdlib.h>
#include <pthread.h> 

/* global variables (shared by all threads */
volatile long double pi = 0.0; /* the approximation, to 31 sigfigs */
pthread_mutex_t piLock;        /* how we synchronize writes to 'pi' */
long double intervals;         /* how finely we chop the integration */
int numThreads;                /* how many threads we use */

/* the function a thread executes
 * Parameters: arg, a void* storing the address of the thread ID.
 */
void *computePI(void *id)
{
    long double x,
                width,
                localSum = 0;
    int i,
        threadID = *((int*)id);

    width = 1.0 / intervals;

    for(i = threadID ; i < intervals; i += numThreads) {
        x = (i+0.5) * width;
        localSum += 4.0 / (1.0 + x*x);
    }

    localSum *= width; 

    pthread_mutex_lock(&piLock);
    pi += localSum;
    pthread_mutex_unlock(&piLock); 

    return NULL;
} 

int main(int argc, char **argv)
{
    pthread_t *threads;        /* dynarray of threads */
    void *retval;              /* unused; required for join() */
    int *threadID;             /* dynarray of thread id #s */
    int i;                     /* loop control variable */

   if (argc == 3) {
      intervals = atoi(argv[1]); 
      numThreads = atoi(argv[2]); 
      threads = malloc(numThreads*sizeof(pthread_t));
      threadID = malloc(numThreads*sizeof(int));
      pthread_mutex_init(&piLock, NULL);

      for (i = 0; i < numThreads; i++) {
        threadID[i] = i;
        pthread_create(&threads[i], NULL, computePI, threadID+i);
      }

      for (i = 0; i < numThreads; i++) {
         pthread_join(threads[i], &retval);
      }
      printf("Estimation of pi is %32.30Lf \n", pi);
      printf("(actual pi value is 3.141592653589793238462643383279...)\n");
    } else {
      printf("Usage: ./a.out <numIntervals> <numThreads>");    
    }

    return 0;
}
kikakater ist offline   Mit Zitat antworten
 


Aktive Benutzer in diesem Thema: 1 (Registrierte Benutzer: 0, Gäste: 1)
 

Forumregeln
Es ist Ihnen nicht erlaubt, neue Themen zu verfassen.
Es ist Ihnen nicht erlaubt, auf Beiträge zu antworten.
Es ist Ihnen nicht erlaubt, Anhänge hochzuladen.
Es ist Ihnen nicht erlaubt, Ihre Beiträge zu bearbeiten.

BB-Code ist an.
Smileys sind an.
[IMG] Code ist an.
HTML-Code ist aus.

Gehe zu


Alle Zeitangaben in WEZ +2. Es ist jetzt 15:20 Uhr.


Powered by vBulletin® Copyright ©2000 - 2026, Jelsoft Enterprises Ltd.
Forum SEO by Zoints
© 2009 FSL Verlag