Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
6004CEM_Week_3_Lab_Tasks/critical_section.cpp
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
42 lines (34 sloc)
1.07 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "omp.h" | |
#include <iostream> | |
#include <cmath> | |
#include <cstdlib> | |
#include <string> | |
#include "stdio.h" | |
#define SIZE 10 | |
/** | |
* Critical sections are OpenMP constructs we use to prevent multiple threads from accessing | |
* the critical section's code at the same time. In simpler words, a critical section specifies | |
* code which is to be executed by only one thread at a time. | |
*/ | |
/** | |
* @brief A function that generates a random array of SIZE 10 and returns the biggest number in that array | |
* @note Makes use of OpenMP | |
*/ | |
int main() | |
{ | |
int i, max, a[SIZE]; | |
for(i=0; i<SIZE; i++) | |
{ | |
a[i] = rand(); | |
std::cout<<a[i]<<std::endl; | |
} | |
max = a[0]; | |
/** | |
* @TODO: Find the biggest number in the array 'a' by utilising OpenMP | |
* @note Use 2 directives | |
* -> the first should assign 4 threads to run at the same time | |
* -> the second one should use the OMP critical statement (applicable only when avoiding Race Conditions) | |
*/ | |
std::cout<<"Max ="<<max<<std::endl; | |
return 0; | |
} |