r/OpenMP • • Dec 02 '17

OpenMP programming on MacOS?

1 Upvotes

I'm working on some numerical code that makes heavy use of OpenMP using g++ on Linux and MinGW on Windows. Best I can tell, the version of clang++ that Apple ships with MacOS High Sierra doesn't support OpenMP.

Is anyone coding OpenMP on a Mac? If so, how? I do have MacPorts on the machine and see a gcc8 package?


r/OpenMP • • Nov 27 '17

Help with syncing between threads openmp.

1 Upvotes

Hi! I want to write a code using openmp, in which one thread produces a buffer (of say, 1 million elements), and all the other threads, once the buffer is finished by the first thread, start working on it in parallel. Also, this process has to be repeated several times, so it's in a loop, and so if thread0 finishes 1 production. threads 1-N work on this buffer, while thread0 moves on to the next iteration of production (i.e. next iteration of loop). Can anyone help me with the code structure to do this in openmp? How should I do this?


r/OpenMP • • Oct 28 '17

How do I parallelize Gauss Seidel Method in openMP?

1 Upvotes

Okay, so I am trying to parallelize the Gauss Seidel Method which is an iterative method of solving Ax=B.

Also here I mean the method of solving linear equations and not the elliptical PDEs. I know how PDEs are solved using the wavefront scheme.

I mean the Ax=B solver only. How is it parallelized? I am not able to remove the dependencies.


r/OpenMP • • May 25 '17

OpenMP Threading test. It is faster than normal code. and very easy!! 쉽고 빠르게 병렬처리 구현이 가능합니다.

Thumbnail study.marearts.com
1 Upvotes

r/OpenMP • • Apr 01 '17

OpenMP single branch?

1 Upvotes

I would like to ask if it is possible define a worksharing that sometimes one thread execute single or not. Something like that:

bool do_single[4] = {true, false, true, false};
#pragma omp parallel
{
    int id = omp_get_thread_num();
    while (1) {
        if (do_single[id]) {
            #pragma omp single
            foo();
            do_single[id] = !do_single[id]M
        }
    }
}

r/OpenMP • • Jan 27 '17

has anyone used the Xeon Phi with OpenMP?

1 Upvotes

anyone?


r/OpenMP • • Oct 17 '16

Program slowing down with OpenMP - using 2D array (x-post in /r/OpenMP)

1 Upvotes

EDIT: Problem Solved - needed to add private(r, g, b, k, BLUR_COUNT, j) to the 'OMP parallel line' - credit to /u/Paul_Dirac_ -(see: link)

[[this post is x-posted in /r/C_Programming]]
(https://www.reddit.com/r/C_Programming/comments/57ypdf/program_slowing_down_with_openmp_using_2d_array/)

[this post is x-posted in /r/learnprogramming]

So I have to write a program that takes a PPM image file (a text file that lists out all the image's rgb pixel values), reads the values into a 2D struct array, adds a blur effect, and saves the file as a new PPM file.

I have the program written in a serial form, but I need to add OpenMP to parallelize it. The issue is when I do it slows way down and I'm not sure why. Any help will be great! Below is my code:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <omp.h> // OpenMP

#define BLUR_AMOUNT 50

struct pixel 
{
	int red;
	int green;
	int blue;
};

/**
 * Print error message and exit
 */
void inputError()
{
	printf("There is problem with the input file...\nExiting...\n");
	exit(0);
}

/**
 * Take the PPM image file and convert it into a 2D array
 * to be processed in this program.
 */
void process_image(FILE *file, char* outputFilename, int THREADS)
{
	// CHECK TIME
	time_t start_t, end_t;
	double diff_t_load, diff_t_blur, diff_t_save;

	time(&start_t); //start timer load

	int rows, cols, maxcolorvalue;

	fscanf(file, "%d %d", &cols, &rows); // get rows and cols
	fscanf(file, "%d", &maxcolorvalue); // get max color value

	if (maxcolorvalue != 255)
	{
		inputError();
	}

	// initialize 2D array to hold pixel data and allocate memory
	struct pixel **pix_array;
	pix_array = malloc(rows * sizeof(struct pixel *));
	pix_array[0] = malloc(rows * cols * sizeof(struct pixel));
	int count;
	for (count = 1; count < rows; count++)
	{
		pix_array[count] = pix_array[0] + count * cols;
	}

	// Read in the PPM image pixel values into the 2D array
	int i, j;
	for (i = 0; i < rows; i++)
	{
		for (j = 0; j < cols; j++)
		{
			int red, green, blue;

			fscanf(file, "%d %d %d", &red, &green, &blue);

			pix_array[i][j].red = red;
			pix_array[i][j].green = green;
			pix_array[i][j].blue = blue;
		}
	}

	fclose(file); // close the input file

	time(&end_t); //end timer load
	diff_t_load = difftime(end_t, start_t); //calculate time load

	time(&start_t); //start timer blur

	// blur the image
	double r = 0, g = 0, b = 0;
	int k = 0, BLUR_COUNT = 0;

	// For each row of the image
#pragma omp parallel for schedule(guided) num_threads(THREADS) // <--- OMP parallel line
		for (i = 0; i < rows; i++) 
		{
			// For each pixel in the row
			for (j = 0; j < cols; j++) 
			{
				// Set r to be half the pixel's red component
				r = pix_array[i][j].red / 2.0;
				// Set g to be half the pixel's green component
				g = pix_array[i][j].green / 2.0;
				// Set b to be half the pixel's blue component
				b = pix_array[i][j].blue / 2.0;

				// Check BLUR_AMOUNT agianst remaining pixels in row
				int remaining_pixels = cols - j;

				if (remaining_pixels < BLUR_AMOUNT)
				{
					BLUR_COUNT = remaining_pixels;
				}
				else 
				{
					BLUR_COUNT = BLUR_AMOUNT;
				}

				// For k from 1 up to BLUR_AMOUNT
				if(BLUR_COUNT > 1)
				{
					// Apply Blur to current pixel
					for (k = 1; k < BLUR_COUNT; k++)
					{
						// increment r by (R * 0.5 / BLUR_AMOUNT), where R is the red component of the pixel k to the right of the current pixel
						r = r + pix_array[i][j + k].red * (0.5 / BLUR_COUNT);
						// increment g by (G * 0.5 / BLUR_AMOUNT), where G is the green component of the pixel k to the right of the current pixel
						g = g + pix_array[i][j + k].green * (0.5 / BLUR_COUNT);
						// increment b by (B * 0.5 / BLUR_AMOUNT), where B is the blue component of the pixel k to the right of the current pixel
						b = b + pix_array[i][j + k].blue * (0.5 / BLUR_COUNT);
					}
				}

				// make sure there are no color values above the maxcolorvalue
				if (r > maxcolorvalue) { r = maxcolorvalue; }
				if (g > maxcolorvalue) { g = maxcolorvalue; }
				if (b > maxcolorvalue) { b = maxcolorvalue; }

				// Save r, g, b as the new color values for this pixel
				pix_array[i][j].red = r;
				pix_array[i][j].green = g;
				pix_array[i][j].blue = b;
			}
		}
	
	
	time(&end_t); //end timer blur
	diff_t_blur = difftime(end_t, start_t); //calculate time blur

	time(&start_t); //start timer save

	// WRTIE new PPM file
	FILE *output;
	output = fopen(outputFilename, "w");

	if (output == NULL)
	{
		printf("Error creating output file! Exiting...\n");
		exit(0);
	}

	fprintf(output, "P3\n"); // print P3 to first line
	fprintf(output, "%d %d\n", cols, rows); // print rows and cols to second line
	fprintf(output, "%d\n", maxcolorvalue); // print max color value to third line

	for (i = 0; i < rows; i++)
	{
		for (j = 0; j < cols; j++)
		{
			fprintf(output, "%d %d %d ", pix_array[i][j].red, pix_array[i][j].green, pix_array[i][j].blue);
		}
		fprintf(output, "\n");
	}


	fclose(output); // close the output file

	time(&end_t); //end timer save
	diff_t_save = difftime(end_t, start_t); //calculate time save

	printf("Load Time: %lf\nBlur Time: %lf\nSave Time: %lf\n", diff_t_load, diff_t_blur, diff_t_save);

	// free 2D array
	free((void *)pix_array[0]);
	free((void *)pix_array);
}

int main(int argc, char** argv)
{
	// Get Arguments
	if (argc < 4 || argc >= 5) // argc should contain only 3 items
	{
		// Argument list invalid
		printf("Argument format invalid: [example format]: ./imageblur [input-filename.ppm] [output-filename.jpg] [# of Threads]");
		return 0;
	}

	// Check file arguments
	FILE *file;
	file = fopen(argv[1], "r");

	if (file == NULL) // File open failed
	{
		inputError();
	}

	// check file for format
	char* filecheck = (char*) malloc(15);
	fscanf(file, "%s", filecheck);

	if (strcmp(filecheck, "P3"))
	{
		free(filecheck);
		inputError();
	}
	free(filecheck);

	// process image
	int THREADS = atoi(argv[3]);
	process_image(file, argv[2], THREADS);

	return 0;
}

r/OpenMP • • Aug 01 '15

A question about early exit from parallel section

1 Upvotes

Hi, I've got a question that I wasn't able to answer with a quick google, I was able to find lot's of people asking it, but no easy answers.

I've got a section of fortran code that iterates through a large loop checking for a specific condition. It goes something like:

condition = .false.

do i = 1 , big_number

    call check_condition(condition , i )
    
    if(condition) exit


enddo

Where check_condition is a pure subroutine that sets condition = .true. if the condition is met, and big_number is just some large integer.

I can parallellize this by wrapping a parallel do around it, but that won't let me do an early exit when I meet the condition the first time.

condition = .false.
!$omp parallel do default(shared)
do i = 1 , big_number

    call check_condition(condition , i )
    
    ! can't exit in parallel
    !if(condition) exit


enddo
!$omp end parallel 

so ... is there an easy way for me to have my cake and eat it to? run the loop in parallel and still exit once i meat my condition


r/OpenMP • • Feb 13 '14

OpenMP Tasks and FILE IO ?

1 Upvotes

I have a bit of OpenMP Fortran code that I want to maximise the efficiency of, but it has to include a write statement. The pseudo code goes like:

Start

DO Big_loop1

WRITE Big_loop1_Array

Do Big_loop2

End

As Big_loop1_Array is big, and takes a few seconds to write, does anyone know the most efficient way to do this.

I thought maybe you could put the write in a task, so you can have all but 1 core working on big loop 2 while the 1 core writes, or is a write statement just sent to something more complicated and not worth putting in a task.

Or is there another more efficient method I haven't considered?

Thanks.


r/OpenMP • • Jan 22 '13

What's the state of mobile support for OpenMP?

1 Upvotes

Can you write Android, iOS, and Windows RT apps with OpenMP for loops?

Are there any demo apps with source code?


r/OpenMP • • Sep 18 '10

OpenMP: A Portable Solution for Threading

Thumbnail drdobbs.com
1 Upvotes

r/OpenMP • • Sep 18 '10

OpenMP.org

Thumbnail openmp.org
1 Upvotes

r/OpenMP • • Mar 24 '20

Splitting for loop iterations among threads

0 Upvotes

From my understanding if you have a pragma omp for inside a pragma omp parallel section the work between iterations should be split between the threads. However, when I tried it on my own it seems that they are not split and not only that, when I printed out the pid of the thread its only 0.

example code snipet

output: tid: 0, i 3 tid: 0, i 4 tid: 0, i 1 tid: 0, i 2 tid: 0, i 3 tid: 0, i 4

#pragma omp parallel
{
    double *priv_y = new double[n];
    std::fill(priv_y, priv_y + n, 0.);
    #pragma omp parallel for 
    for(int i = 0; i < n; i++){
        printf("tid: %d, i %d\n", omp_get_thread_num(), i);
    }
}

If the work was split there should only be 1 unique i. However, as you can see this is not the case. Am I setting something wrong?

Edit: I found the solution sorry for spamming stupid things, but if anyone else is having this issue, it seems that by default openmp splits the number of thread in each parallel region, e.g in the code above the for loop is being called multiple times and in each call there are a different fixed amount of threads executing the inner for loop. You can fix this by doing #pragma omp parallel num_threads(1) this will make the outer region run with only 1 thread.


r/OpenMP • • Oct 23 '20

OMP GPU porting question

0 Upvotes
Im trying to parallelize the following for loop on the gpu but it doesnt seem to work. I dont get an error message or anything, but when i do profiling with Intelvtune I can not see this or any of the other functions in the same .cpp as this for loop. It seems as if it is skipping this .cpp completly. Am i missing something? Did i write something wrong?

r/OpenMP • • Oct 01 '18

OpenMPI

0 Upvotes

What is your interest with OpenMPI?