[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Gomp-discuss] A Simple PThread Example of OpenMP
From: |
Scott Robert Ladd |
Subject: |
[Gomp-discuss] A Simple PThread Example of OpenMP |
Date: |
Thu, 08 Apr 2004 11:58:17 -0400 |
User-agent: |
Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.6) Gecko/20040401 Debian/1.6-4 |
Here's an example of how a simple OpenMP program can be implemented
using pthreads. In toher words, GOMP should convert:
#include <stdio.h>
int main(void)
{
#pragma omp parallel
puts("Hello, World!\n");
return 0;
}
into this code that uses pthreads:
#include <stdio.h>
#include <pthread.h>
#include <unistd.h>
// this function encapsulates the paralle region defined
// by the OpenMP pragma
void * par_proc(void * arg)
{
puts("Hello, World!\n");
return NULL;
}
int main(void)
{
// what we'd do in a real OpenMP program
// #pragma omp parallel
// puts("Hello, World!\n");
// Linux-specific; OpenMP would also check environment settings
long num_procs = sysconf(_SC_NPROCESSORS_CONF);
int n;
// an array of thread ids
pthread_t * thread_ids = (pthread_t *)malloc(sizeof(pthread_t)
* num_procs);
// for each processor...
for (n = 0; n < num_procs; ++n)
{
// launch a thread, storing its id in the list
pthread_create(&thread_ids[n],NULL,par_proc,NULL);
}
// wait for threads to finish
for (n = 0; n < num_procs; ++n)
{
// wait for each thread in succession
pthread_join(thread_ids[n],NULL);
}
// clean up and exit
free(thread_ids);
return 0;
}
Does this make sense to everyone?
The example is Linux-specific; however, I think that only the sysconf
call might need to be replaced on other pthread-enabled OSs.
--
Scott Robert Ladd
Coyote Gulch Productions (http://www.coyotegulch.com)
Software Invention for High-Performance Computing
- [Gomp-discuss] A Simple PThread Example of OpenMP,
Scott Robert Ladd <=