Pp lab
This presentation is the property of its rightful owner.
Sponsored Links
1 / 8

PP Lab PowerPoint PPT Presentation


  • 67 Views
  • Uploaded on
  • Presentation posted in: General

PP Lab. Open MP programming Lab 10 and 11. THE LOOP directive. The for directive splits the for-loop so that each thread in the current team handles a different portion of the loop. A simple example. #include < omp.h > #include " stdafx.h " void main() { #pragma omp parallel {

Download Presentation

PP Lab

An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

Presentation Transcript


Pp lab

PP Lab

Open MP programming

Lab 10and 11


The loop directive

THE LOOP directive

  • The for directive splits the for-loop so that each thread in the current team handles a different portion of the loop.


A simple example

A simple example

#include <omp.h>

#include "stdafx.h"

void main()

{

#pragma omp parallel

{

#pragma omp for

for(intn=0; n<10; ++n)

{

printf("thread# %d says %d\n", omp_get_thread_num(),n);

}

}

}

Combined syntax: #pragma omp parallel for


Clauses of loop directive

Clauses of loop directive

  • Private

  • Shared

  • Firstprivate

  • Lastprivate

  • Reduction

  • Schedule

  • Ordered

  • Nowait


Pp lab

Example using private and shared

#include <stdio.h>

#include <math.h>

#include <omp.h>

intmain() {

inti, sum=0;

#pragma omp parallel private(i) shared(sum) // every thread has a different i, sum is a global variable

{

#pragma omp for

for (i=1; i<=10; ++i)

{

#pragma ompatomic

//this directive guarantees mutual exclusion on the shared variable, only one thread can write to sum at //a time, threads execute atomically in this region

sum += i; } }

printf(“The sum of 1 through 10 is %d\n", sum); }


Pp lab

printf("using the barrier now\n\n");

#pragma omp parallel

{i=0;

#pragma omp for firstprivate(i) lastprivate(i)

for(n=0; n<10; ++n)

{printf("thread# %d says %d, this is my iteration# %d\n\n", omp_get_thread_num(),n,i);

i++;}

printf("thread# %d says i'm done\n\n",omp_get_thread_num());

#pragma omp single

printf("value of i is %d\n\n",i); } }

#include <omp.h>

#include "stdafx.h"

void main() {

int n, i;

printf("not using the barrier\n\n");

#pragma omp parallel

{ i=0;

#pragma omp for firstprivate(i) nowait

for(n=0; n<10; ++n)

{printf("thread# %d says %d, this is my iteration# %d\n\n", omp_get_thread_num(),n,i);

i++;}

printf("thread# %d says i'm done\n\n",omp_get_thread_num());

}

Example using firstprivate, lastprivate and nowait


Pp lab

Example using schedule

#include <omp.h>

#include "stdafx.h"

void main() {

#pragma omp parallel

{

#pragma omp single

printf("static shedule:\n");

#pragma omp for schedule(static,5)

for(int n=0; n<23; ++n)

printf("thread# %d says %d\n", omp_get_thread_num(),n);

#pragma omp single

printf("dynamic shedule:\n");

#pragma omp for schedule(dynamic,5)

for(int n=0; n<23; ++n)

printf("thread# %d says %d\n", omp_get_thread_num(),n); } }


Pp lab

Example using reduction

#include <omp.h>

#include <stdio.h>

#include <stdlib.h>

#define N 1000

int main (intargc, char *argv[]) {

double a[N]; double sum=0.0; inti,n,tid;

#pragma omp parallel shared(a) private(i)

{tid=omp_get_thread_num();

#pragma omp single

printf("Number of threads = %d\n",omp_get_num_threads());

#pragma omp for

for(i=0;i<N;i++) a[i]=i+1;

#pragma omp for reduction(+:sum)

for(i=0;i<N; i++) sum=sum+a[i]; }/* sums of all threads are added up into one variable */

printf("Sum = %2.1f\n",sum); return(0);}


  • Login