Skip to content

Posix Implementation

Jonathan edited this page Nov 28, 2017 · 2 revisions

We declare a function that takes the thread_id as parameter, initialize the variables for our Image structure, and one for the offset as the Secuential Implementation

void *blur_image(void *threadid) {

	int rows = image.rows;
	int cols = image.cols;
	
	//always radius less than protect, minimun value = 2
	int protect = radius + 1;
	
	int init;
	int final;

	long idthread = (long)threadid;
	int pass = floor(rows/NUM_THREADS);

	if(idthread == 0) {
		//initial thread
		init = protect;
		final = pass;
	}
	else if(idthread == NUM_THREADS - 1) {
		//final thread
		init = (idthread * pass) + 1;
		final = rows - protect;
	}else {
		init = (idthread * pass) + 1;
		final = init + pass - 1;
	}


	for (int i = init; i <= final; ++i)
	{
		for (int j = protect; j < image.cols - protect; ++j)
		{

			//(i+1,j) down
			//(i-1,j) top
			//(i,j+1) right
			//(i,j-1) left

			//RED
			blur.at<Vec3b>(i,j)[0] = (image.at<Vec3b>(i+radius,j)[0] + image.at<Vec3b>(i-radius,j)[0] + image.at<Vec3b>(i,j+radius)[0] + image.at<Vec3b>(i,j-radius)[0])/4;

			//GREEN
			blur.at<Vec3b>(i,j)[1] = (image.at<Vec3b>(i+radius,j)[1] + image.at<Vec3b>(i-radius,j)[1] + image.at<Vec3b>(i,j+radius)[1] + image.at<Vec3b>(i,j-radius)[1])/4;

			//BLUE
			blur.at<Vec3b>(i,j)[2] = (image.at<Vec3b>(i+radius,j)[2] + image.at<Vec3b>(i-radius,j)[2] + image.at<Vec3b>(i,j+radius)[2] + image.at<Vec3b>(i,j-radius)[2])/4;
		}
	}
} 

Now in the main, we read the image

char* imageName = argv[1];
image = cv::imread( imageName, cv::IMREAD_COLOR );
if( !image.data )
{
	perror("No image data \n");	
}

We read the arguments of kernel radius and number of threads given, if available

if (argv[2]==NULL || argc<3) {
	radius = 3;
	NUM_THREADS = 4;
	std::cout<<"Radius and NUM_THREADS are null"<<std::endl;
}
else {
	radius = atoi(argv[2]);
}

if (argv[3]==NULL || argc<4) {
	NUM_THREADS = 4;
	std::cout<<"NUM_THREADS is null"<<std::endl;
}
else {
	NUM_THREADS = atoi(argv[3]);
}

We prepare the image for executing the blurring, and take the time

blur = image.clone();

//take the time
struct timespec start, finish;
double elapsed;

clock_gettime(CLOCK_MONOTONIC, &start);

We launch our POSIX threads

pthread_t threads[NUM_THREADS];
int rc;
long i;

for (i = 0; i < NUM_THREADS; ++i)
{
	rc = pthread_create(&threads[i], NULL, blur_image, (void*) i);
	if(rc)
		perror("Error: we cannot create thread");
}

we synchronize the threads after execution

//this join increases runtime
for (i = 0; i < NUM_THREADS; ++i)
pthread_join(threads[i], NULL);

clock_gettime(CLOCK_MONOTONIC, &finish);
elapsed = (finish.tv_sec - start.tv_sec);
elapsed += (finish.tv_nsec - start.tv_nsec) / 1000000000.0;
std::cout<<"Time: "<< elapsed << std::endl;

And finally write the output for future analysis:

//write in file
std::ofstream output;
output.open("mainposixResult.txt", std::ofstream::app | std::ofstream::out );
output<< imageName <<", "<< radius << ", " << NUM_THREADS <<", "<< elapsed << std::endl;
output.close();

Blur

POSIX Threads (pthreads)

Threads (openMP)

GPU (cuda)

How to run

Clone this wiki locally