0

Im coding an aplication that runs the Monaco algorithm using different thread numbers, them being 2,4,6 and 8 to calculate the value of PI. The objective is too see a speed increase when using more threads. The problem is, my code is not dividing the work, but simply doing the work 2 times!

This is my code:

#include <stdio.h>
#include <stdlib.h>
#include <pthread.h> 
#include <time.h> 
#include <math.h>
#include <string.h>
#define SEED 35791246


void *monaco(int arcg, char* argv) {

  clock_t t1, t2;
  float pi;
  t1 = clock();
  t2 = clock();


  float diff = ((float)(t2 - t1) / 1000000.0F );
  printf("\n");
  printf("Thread number = %d\n", (int)(pthread_self()));
  printf("Total Time: %f segundos\n", diff);
  printf("\n");
  printf("\n");
  return 0;
}


void run_test(int num_thread) {
  pthread_t pth_arr[num_thread];
  int i = 0;
  for (i = 0; i < num_thread; i++) {
    pthread_create(&pth_arr[i], NULL, monaco, NULL);
  }

  for (i = 0; i < num_thread; i++) {
    pthread_join(pth_arr[i], NULL);
  }
}


int main(count) {
  int num_thread = 9;
  int pontos=20000;
  double x,y;
  count=0; 
  double z;
  float pi;
  int j = 0;
  int i = 0;
  srand(SEED);
  count = 0;
  for (i = 1; i < num_thread; i++) {
    if (i==1) 
    {
      continue;
    }
    if (i==3) 
    {
      continue;
    }
    if (i==5) 
    {
      continue;
    }
    if (i==7) 
    {
      continue;
    }
      for (j = 0; j<pontos; j++) {
        x = (double)rand()/RAND_MAX;
        y = (double)rand()/RAND_MAX;
        z = x*x+y*y;
        if (z<=1) count++;
      }
      pi=(double)count/pontos * 4;
      printf ("Points used are: %d , and the Pi estimate is: %g \n", pontos,pi);
    printf ("Using %d threads.\n", i);
    run_test(i);
  }
  return 0;
}

The output I get is:

Points used are: 20000 , and the Pi estimate is: 3.1288                                                                          
Using 2 threads.                                                                                                                          

Thread number = -1332082944                                                                                                                  
Total Time: 0.000002 segundos                                                                                                                  



Thread number = -1340475648                                                                                                                  
Total Time: 0.000002 segundos 

Thread number = -1340475648                                                                                                                  
Total Time: 0.000002 segundos                                                                                                                  


Points used are: 20000, and the Pi estimate is: 6.2932                                                                          
Using 4 threads.                                                                                                                          

Thread number = -1332082944                                                                                                                  
Total time: 0.000002 segundos                                                                                                                  


Thread number = -1348868352                                                                                                                  
Total time: 0.000001 segundos                                                                                                                  


Thread number = -1357261056                                                                                                                  
Total time: 0.000001 segundos                                                                                                                  



Thread number = -1340475648                                                                                                                  
Total time: 0.000002 segundos                                                                                                                  


Points used are: 20000 , and the Pi estimate is: 9.4232

How can I make it divide the work instead of doing it 2 times?

1 Answer 1

1
  • Compiler Warnings

In the early stages of learning (maybe even if you are an expert), you need to presume that the compiler is smarter than you. When I compile your code as it is, I got the following errors:

SO.c: In function ‘run_test’:
SO.c:32:39: warning: passing argument 3 of ‘pthread_create’ from incompatible pointer type [-Wincompatible-pointer-types]
   32 |     pthread_create(&pth_arr[i], NULL, monaco, NULL);
      |                                       ^~~~~~
      |                                       |
      |                                       void * (*)(int,  char *)
In file included from SO.c:3:
/usr/include/pthread.h:236:15: note: expected ‘void * (*)(void *)’ but argument is of type ‘void * (*)(int,  char *)’
  236 |       void *(*__start_routine) (void *),
      |       ~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
SO.c: In function ‘main’:
SO.c:41:5: warning: type of ‘count’ defaults to ‘int’ [-Wimplicit-int]
   41 | int main(count) {
      |     ^~~~

In the first set of errors, the compiler is reporting about the incompatible pointer argument. From the pthreads man page:

int pthread_create(pthread_t *thread, const pthread_attr_t *attr,
                          void *(*start_routine) (void *), void *arg);

pthread_create() takes void* (*func) (void*) function pointer as an argument, but the monaco() function you passed is of type void* (*monaco) (int, char*).

If you want to pass multiple arguments to the start_routine(), you need to think of some alternative approaches, for example, packing them in a structure and passing its address as an argument.

And about the second set of errors, I am surprised to see your code compile in the first place. I am not aware of the implications of such declaration, and someone else needs to answer :(

  • The logic

Even after correcting all the syntax errors, I am not sure how your code will divide the work between threads. You are doing most of the computation in the main(). Maybe you are in the initial version of the code, and I don't want to spoil the fun by providing the full code :)

And for the error part, for each iteration, the value is doubling because you are not reinitializing the count to 0 at the start of the iteration. With this line, count = 0; at the beginning of the for loop, the output is as expected, but remember by no means your code is acceptable in the current state.

Tip: i & 1 is an easy way to determine the odd number.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.