gomp-discuss
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Gomp-discuss] OpenMP: Examples 1


From: Scott Robert Ladd
Subject: [Gomp-discuss] OpenMP: Examples 1
Date: Thu, 26 Aug 2004 13:28:41 -0400
User-agent: Mozilla Thunderbird 0.7.3 (X11/20040812)

Okay,

Attached hereto are the most basic examples: A "Hello, world" program in OpenMP, and as implement with PThreads and Unix-style processes. I also have an MPI and Windows Processes version, but those are on a machine that is busy at the moment.

Yes, this is incredible simple -- which is my point. We need to figure out what the baseline is for doing even the simplest program. What is common between different parallelization models?

Later tonight, I'll pull a more advanced example (a basic loop) from CVS.

My goal was pretty simple: To see precisely what sort of code we would expect under different conditions, so we could identify the common features for constructing a universal model.

I largely gave up on GOMP earlier this year, due to a lack of interest by others.

..Scott

--
Scott Robert Ladd
Coyote Gulch Productions (http://www.coyotegulch.com)
Software Invention for High-Performance Computing
#include "stdio.h"

int main(void)
{
    #pragma omp parallel
    puts("Hello, World!\n");
    
    return 0;
}
#include <stdio.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h> 

int main(void)
{
//    #pragma omp parallel
    long num_procs = sysconf(_SC_NPROCESSORS_CONF);
    
    printf("number of processors = %ld\n",num_procs);
    
    long n;
    
    for (n = 0; n < num_procs; ++n)
    {
        // create compile process and wait for it to finish
        pid_t child_pid;
        int child_result;
        child_pid = fork();

        // if this is a child, display our message
        if (child_pid == 0)
        {
            puts("Hello, World!\n");
            exit(0);
        }
    }
    
    // wait for the right number of processes to exit
    for (n = 0; n < num_procs; ++n)
        wait(NULL);    
    
    return 0;
}
#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 here
    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;
}


reply via email to

[Prev in Thread] Current Thread [Next in Thread]