OpenMP does not terminates [closed] - c

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 3 months ago.
Improve this question
I've took an example of the simple OpenMP code.
#include <stdio.h>
#include <omp.h>
int main(int argc, char** argv){
int i;
int thread_id;
omp_set_num_threads(4);
#pragma omp parallel
{
thread_id = omp_get_thread_num();
for( int i = 0; i < omp_get_num_threads(); i++){
if(i == omp_get_thread_num()){
printf("Hello from process: %d\n", thread_id);
}
#pragma omp barrier
}
}
return 0;
}
Compiled it with gcc -fopenmp omp_test_hello.c -o a.exe.
I did not change or set any environment variables (I've decided that internal directives in code should work similarly). When I execute file I get the following output:
Hello from process: 0
Hello from process: 3
Hello from process: 3
Hello from process: 3
After that, the execution of the program does not stop, so it seems that something blocks it from termination.
I've tried even more simple example of the code without the barrier and for loop. It does not terminates similarly, though the output include "signals" from all threads:
int main(int argc, char** argv){
int i;
int thread_id;
omp_set_num_threads(4);
#pragma omp parallel
{
thread_id = omp_get_thread_num();
printf("Hello from process: %d\n", thread_id);
}
return 0;
}
Output:
Hello from process: 2
Hello from process: 1
Hello from process: 0
Hello from process: 3
I've managed to test these examples also on Linux and get same results.
So what could be the problem?

Problem solved by reinstalling mingw compiler.

Related

Xcode error message not making logical sense [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 1 year ago.
Improve this question
I'm supposed to be generating a program that creates a thread that generates a random number between 0 and 1 and prints out the generated random number.
The error messages I'm receiving from Xcode don't make any logical sense. I tried many different methods, I might just be overlooking it but I could don't see any possible syntax errors
#include <stdio.h>
#include <pthread.h>
#include <stdlib.h>
void printMsg(char* msg) {
int status = 0;
printf("%s\n", msg);
pthread_exit(&status);
}
int main(int argc, char** argv) {
pthread_t thrdID;
int* status = (int*)malloc(sizeof(int));
printf("creating a new thread\n");
pthread_create(&thrdID, NULL, (void*)printMsg, argv[1]);
printf("created thread %d\n". thrdID);
pthread_join(thrdID, &status);
printf("Thread %d exited with status %d\n", thrdID, *status);
return 0;
}
Oh, I see it now, you had a period instead of a comma here:
printf("created thread %d\n". thrdID);
should be
printf("created thread %d\n", thrdID);
It was pointed out in some now deleted comments, that there were other issues with the code that should be corrected.
These are some suggestions about how to deal with these issues:
#include <stdio.h>
#include <pthread.h>
#include <stdlib.h>
void printMsg(char* msg) {
// The stack of the thread will be destryed on exit
// so you need to allocate the status value (or do some
// rather dubious casting... this is cleaner).
int* status_pointer = malloc(sizeof(int));
printf("%s\n", msg);
// For demonstration purposes set it to something other than 0.
*status_pointer = 123;
pthread_exit(status_pointer);
}
int main(int argc, char** argv) {
pthread_t thrdID;
void* status = NULL;
printf("creating a new thread\n");
// You should not blindly pass argv[1] without checking it's valid.
// You can provide a default value like I did below, or error out.
char *msg = argc > 1 ? argv[1] : "This is a default message.";
pthread_create(&thrdID, NULL, (void*)printMsg, msg);
// thrdId is pointer so use the %p format specifier.
printf("created thread %p\n", thrdID);
pthread_join(thrdID, &status);
// same issue with %p
printf("Thread %p exited with status %d\n", thrdID, *(int*)status);
// don't forget to free what you allocated.
free(status);
return 0;
}

Why pthread.h library is not exist in Ubuntu? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
#include <pthread.h>
#include <stdio.h>
int sum;
void *runner(void *param); /* threads call this function */
int main(int argc, char *argv[])
{
pthread_t tid; /* the thread identifier */
pthread_attr_t attr; /* set of thread attributes */
if(argc != 2) {
fprintf(stderr, "usage: a.out <integer value>\n");
return -1;
}
if(atoi(argv[1]) < 0) {
fprintf(stderr, "%d must be >= 0\n", atoi(argv[1]));
return -1;
}
/* get the default attributes */
pthread_attr_init(&attr);
/* create the thread */
pthread_create(&tid, &attr, runner, argv[1]);
/* wait for the thread to exit */
pthread_join(tid, NULL);
printf("sum = %d\n", sum);
}
/* The thread will begin control in this function */
void *runner(void *param)
{
int i, upper = atoi(param);
sum = 0;
for(i=1; i <= upper; i++) sum += i;
pthread_exit(0);
}
This is my code following book.
Make child thread and allocate the runner function to cumulative the values in array.
But gcc Multithread_Pthread.c isn't work and gcc -pthread Multithread_Pthread.c are also work not.
What is the reason and solution?
Oh I forget the result of each command.
Use the gcc Multithread_Pthread.c error has occurred.
Use the gcc -pthread Multithread_Pthread.c usage: a.out
is the result.
Try this:
$ gcc Multithread_Pthread.c -lpthread
To complement Ren's answer: you should try to compile object files before linking them instead of building the whole program with a single command. Try this:
gcc -c Multithread_Pthread.c
This will correctly create a "Multithread_Pthread.o" object file, proving that "pthread.h" was found in system header files.
Next is command to try linking:
gcc Multithread_Pthread.o
You will get the same message error, proving you this is a linking error.

Change source file from dynamic scheduling to static scheduling

I'm trying to change the following C source code to static scheduling, but I don't know how it's done. I've tried to staticbefore #pragma omp parallel private(nthreads, tid):
#include <omp.h>
#include <stdio.h>
#include <stdlib.h>
int main (int argc, char *argv[])
{
int nthreads, tid;
/* Fork a team of threads giving them their own copies of variables */
static #pragma omp parallel private(nthreads, tid) //I tried it here
{
/* Obtain thread number */
tid = omp_get_thread_num();
printf("Hello World from thread = %d\n", tid);
/* Only master thread does this */
if (tid == 0)
{
nthreads = omp_get_num_threads();
printf("Number of threads = %d\n", nthreads);
}
} /* All threads join master thread and disband */
}
What I expect is that thread 0 gets the first chunk, thread 1 the second chunk, and so on. However, it's randomly now as it's dynamically

How to wrap OpenMP directives (#pragmas) as a function or function-like macro?

I'm trying to interface OpenMP with another language that emits C code (a C code generator). From my perspective (I'm not the designer of the other language), it will be easiest to do this by calling a C function or function-like macro instead of using #pragma or _Pragma directly. I do not have much experience with the C preprocessor, but I have gotten a simple example found on wikipedia to work in a non-satisfactory way. Here is the original C example:
#include <omp.h>
#include <stdio.h>
#include <stdlib.h>
int main (int argc, char *argv[]) {
int th_id, nthreads;
#pragma omp parallel private(th_id)
{
th_id = omp_get_thread_num();
printf("Hello World from thread %d\n", th_id);
#pragma omp barrier
if ( th_id == 0 ) {
nthreads = omp_get_num_threads();
printf("There are %d threads\n",nthreads);
}
}
return EXIT_SUCCESS;
}
Now here is where I create a macro (pragma_omp_parallel_private) to do what I want:
#include <omp.h>
#include <stdio.h>
#include <stdlib.h>
#define STR(x) #x
#define STRINGIFY(x) STR(x)
#define omp_par_pri STRINGIFY(omp parallel private)
char omp_test[] = omp_par_pri;
#define pragma_omp_parallel_private(thread_id) \
_Pragma(STRINGIFY(omp parallel private ## (thread_id)))
int main (int argc, char *argv[]) {
int th_id, nthreads;
// #pragma omp parallel private(th_id)
pragma_omp_parallel_private(th_id)
{
th_id = omp_get_thread_num();
printf("Hello World from thread %d\n", th_id);
#pragma omp barrier
if ( th_id == 0 ) {
nthreads = omp_get_num_threads();
printf("There are %d threads\n",nthreads);
}
}
return EXIT_SUCCESS;
}
The preprocessor will emit an error, but if I actually compile the preprocessed code, it works as expected. Is there a more legit way to accomplish this (not to mention a way that wouldn't cause build errors in most build pipelines)?
$ gcc -E -fopenmp wikiHello.c > wikiHello_pp.c
wikiHello.c:11:34: error: pasting "private" and "(" does not give a valid preprocessing token
_Pragma(STRINGIFY(omp parallel private ## (thread_id)))
^
wikiHello.c:17:3: note: in expansion of macro ‘pragma_omp_parallel_private’
pragma_omp_parallel_private(th_id)
^
$ gcc -fopenmp wikiHello_pp.c
$ ./a.exe
Hello World from thread 3
Hello World from thread 6
Hello World from thread 4
Hello World from thread 7
Hello World from thread 1
Hello World from thread 5
Hello World from thread 2
Hello World from thread 0
There are 8 threads
I would concatenate the pragma before turning it to a string literal. Something like this:
#include <omp.h>
#include <stdio.h>
#include <stdlib.h>
#define STR(x) #x
#define STRINGIFY(x) STR(x)
#define CONCATENATE(X,Y) X ( Y )
#define pragma_omp_parallel_private(x) \
_Pragma( STRINGIFY( CONCATENATE(omp parallel private,x) ) )
int main (int argc, char *argv[]) {
int th_id, nthreads;
// #pragma omp parallel private(th_id)
pragma_omp_parallel_private(th_id)
{
th_id = omp_get_thread_num();
printf("Hello World from thread %d\n", th_id);
#pragma omp barrier
if ( th_id == 0 ) {
nthreads = omp_get_num_threads();
printf("There are %d threads\n",nthreads);
}
}
return EXIT_SUCCESS;
}

C Programing Multiple Threads using pthread.h [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I'm trying to learning C Programing and multiple threads. I started programing some basic stuff [like the following], but I got stuck. Can someone give me a hand?
program.c
#include <string.h>
#include <stdio.h>
#include <pthread.h>
#define NUM_THREADS 4
void *main_thread(void *threadID) {
long tid;
tid = (long)threadID;
printf("main thread #%ld!\n", tid);
pthread_exit(NULL);
}
void *first_thread(void *threadID) {
long tid;
tid = (long)threadID;
printf("first thread #%ld!\n", tid);
pthread_exit(NULL);
}
void *second_thread(void *threadID) {
long tid;
tid = (long)threadID;
printf("second thread #%ld!\n", tid);
pthread_exit(NULL);
}
void *last_thread(void *threadID) {
long tid;
tid = (long)threadID;
printf("last thread #%ld!\n", tid);
pthread_exit(NULL);
}
int main () {
pthread_t threads[NUM_THREADS];
int rc;
long t;
for (t=0; t < NUM_THREADS; t++) {
printf("In main Function creating thread %ld\n", t);
rc = pthread_create(&threads[t], NULL, first_thread, (void *)t);
if (rc) {
printf("ERROR; Return code from pthread_create is %d\n", rc);
exit(-1);
}
}
pthread_exit(NULL);
return 0;
}
I will keep updating the codes above as i figure new things out
*Hey guys. i did not compile it properly but now i figured out.
gcc -pthread -o main main.c
This code will calculate the value of e^x by calculating terms of its series ,
in each term we need to calculate power of a number(in this program it is x) and factorial of each corresponding power.
since these two calculations are independent so we make two threads for these two functions that will be run parallel.
after calculating values of these two function (end of these two threads) we need to merge the results (i mean power/factorial) and then we add all these results in another parallel thread which will run after these two threads.
#include<stdio.h>
#include<math.h>
#include<pthread.h>
#include<stdlib.h>
long double x,fact[150],pwr[150],s[1];
int i,term;
void *Power(void *temp)
{
int k;
for(k=0;k<150;k++)
{
pwr[k] = pow(x,k);
//printf("%.2Lf\n",pwr[k]);
}
return pwr;
}
void *Fact(void *temp)
{
long double f;
int j;
fact[0] = 1.0;
for(term=1;term<150;term++)
{
f = 1.0;
for(j=term;j>0;j--)
f = f * j;
fact[term] = f;
//printf("%.2Lf\n",fact[term]);
}
return fact;
}
void *Exp(void *temp)
{
int t;
s[0] = 0;
for(t=0;t<150;t++)
s[0] = s[0] + (pwr[t] / fact[t]);
return s;
}
int main(void)
{
pthread_t thread1,thread2,thread3;
printf("Enter the value of x (between 0 to 100) (for calculating exp(x)) : ");
scanf("%Lf",&x);
printf("\nThreads creating.....\n");
pthread_create(&thread1,NULL,Power,NULL); //calling power function
pthread_create(&thread2,NULL,Fact,NULL); //calling factorial function
printf("Threads created\n");
pthread_join(thread1,NULL);
pthread_join(thread2,NULL);
printf("Master thread and terminated threads are joining\n");
printf("Result collected in Master thread\n");
pthread_create(&thread3,NULL,Exp,NULL);
pthread_join(thread3,NULL);
printf("\nValue of exp(%.2Lf) is : %Lf\n\n",x,s[0]);
exit(1);
}

Resources