19
edits
Changes
→Source Code
====Source Code====
// FIRST LINE
#include <iostream>
#include <iomanip>
#include <omp.h>
int funcA_status = true;
int funcB_status = true;
int funcC_status = true;
int funcD_status = true;
int funcE_status = true;
int funcF_status = true;
int funcG_status = true;
int funcH_status = true;
static void A(int tid);
static void B(int tid);
static void C(int tid);
static void D(int tid);
static void E(int tid);
static void F(int tid);
static void G(int tid);
static void H(int tid);
static void J(int tid);
static void K(int tid);
static void L(int tid);
int main(int argc, char** argv) {
omp_set_num_threads(4);
#pragma omp parallel shared(funcA_status, funcB_status, funcC_status, funcD_status, funcE_status, funcF_status, funcG_status, funcH_status)
{
int threadId = omp_get_thread_num();
int numThreads = omp_get_num_threads();
if (threadId == 0)
{
std::cout << "\nnumThreads = " << numThreads << std::endl;
}
if (threadId != 0) {
if (threadId % 2 == 0) {
A(threadId);
}
else {
B(threadId);
}
}
else
{
//J(threadId);
K(threadId);
}
}
// terminate
char c;
std::cout << "Press Enter key to exit ... ";
std::cin.get(c);
}
static void A(int tid) {
printf("Hi from Function A \nThread Num %d\n", tid);
if (funcC_status) {
funcC_status = false;
C(tid);
}
else if (funcD_status) {
funcD_status = false;
D(tid);
}
else if (funcE_status) {
funcE_status = false;
E(tid);
}
else {
K(tid);
}
}
static void B(int tid) {
printf("Hi from Function B \nThread Num %d\n", tid);
if (funcF_status) {
funcF_status = false;
F(tid);
}
else if (funcG_status) {
funcG_status = false;
G(tid);
}
else if (funcH_status) {
funcH_status = false;
H(tid);
}
else {
L(tid);
}
}
static void C(int tid) {
int y = 0;
for (int i = 0; i < INT_MAX / 4; i++)
{
if ((i % 2) == 0)
{
y++;
}
}
std::cout << "Hello from C\n";
#pragma omp barrier
}
static void D(int tid) {
int y = 0;
for (int i = 0; i < INT_MAX / 4; i++)
{
if ((i % 2) == 0)
{
y++;
}
}
std::cout << "Hello from D \n";
#pragma omp barrier
}
static void E(int tid) {
int y = 0;
for (int i = 0; i < INT_MAX / 4; i++)
{
if ((i % 2) == 0)
{
y++;
}
}
std::cout << "Hello from E \n";
#pragma omp barrier
}
static void F(int tid) {
int y = 0;
for (int i = 0; i < INT_MAX; i++)
{
if ((i % 2) == 0)
{
y++;
}
}
std::cout << "Hello from F \n";
}
static void G(int tid) {
int y = 0;
for (int i = 0; i < INT_MAX / 4; i++)
{
if ((i % 2) == 0)
{
y++;
}
}
std::cout << "Hello from G \n";
#pragma omp barrier
}
static void H(int tid) {
int y = 0;
for (int i = 0; i < INT_MAX / 4; i++)
{
if ((i % 2) == 0)
{
y++;
}
}
std::cout << "Hello from H \n";
#pragma omp barrier
}
static void J(int tid) {
int y = 0;
for (int i = 0; i < INT_MAX / 4; i++)
{
if ((i % 2) == 0)
{
y++;
}
}
std::cout << "Hello from J \n";
#pragma omp barrier
}
static void K(int tid) {
int y = 0;
for (int i = 0; i < INT_MAX / 2; i++)
{
if ((i % 2) == 0)
{
y++;
}
}
L(tid);
}
static void L(int tid) {
int y = 0;
for (int i = 0; i < INT_MAX / 4; i++)
{
if ((i % 2) == 0)
{
y++;
}
}
K(tid);
}