We would like to know how many threads a program is using and we would like each thread to say hello to us.
Consider the following program:
#include <cstdio>
#include "omp.h"
int main()
{
printf("Thread #%d reports: This program uses %d threads.\n", omp_get_thread_num(), omp_get_num_threads());
#pragma omp parallel
printf("Hello world from thread #%d\n", omp_get_thread_num());
return 0;
}
Can you make only thread 5 report the correct number of threads?
What is the number of physical cores available on the compute nodes of Hamilton7?
The following is a scrambled message:
Zpv!uijol!zpvs!qbjo!boe!zpvs!ifbsucsfbl!bsf!voqsfdfefoufe!jo!uif!ijtupsz!pg!uif!xpsme-!cvu!uifo!zpv!sfbe/
This code was used for encryption:
#include <cstdio> // Import the printf function
#include "omp.h" // Import the OpenMP library functions
// Start of program
int main()
{
// This is a "character array", consisting of 105 letters in byte form.
char mymessage[] = "This is a placeholder of the original message";
// The message is encoded by shifting all letters by one, i.e.
// A becomes B, c becomes d and so forth. This shift is easy to implement
// in byte form as all you need to do is to add 1 to a character.
// To printf a single character, use e.g.
// printf("%c\n", mymessage[0]);
// To printf the whole array do e.g. (subtle difference between %c and %s)
// printf("%s\n", mymessage);
/* MISSING CODE: for loop over the 105 characters and adding the value 1 to each entry; */
printf("%s\n", mymessage); // This prints a character array to screen
return 0; // Exit program
}
Hint:
Use suitable pragmas and run the decryption thread-parallel.
Insert a statement in the for-loop that prints the current thread number and decryption result.
Run the code repeatedly and vary the number of threads (OMP_NUM_THREADS). What do you observe?