-3

hello guys I'm trying to write Trapezoidal Rule in parallel using C++ it compile with no errors but when I run it gives " Floating point exception (core dumped) " parallels@parallels-Parallels-Virtual-Platform:~/Desktop$ g++ -Wall assignmemt.cpp -o output -lpthread parallels@parallels-Parallels-Virtual-Platform:~/Desktop$ ./output usage: ./output n is the number of terms and should be >= 1 parallels@parallels-Parallels-Virtual-Platform:~/Desktop$ ./output 3 5 Floating point exception (core dumped) parallels@parallels-Parallels-Virtual-Platform:~/Desktop$ ^C parallels@parallels-Parallels-Virtual-Platform:~/Desktop$

   
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <pthread.h>


const int MAX_THREADS = 1024;


long thread_count;
long long n;
double sum;
pthread_mutex_t mutex;

#define f(x) 1/(1+pow(x,2))


void Usage(char* prog_name) {
   fprintf(stderr, "usage: %s <number of threads> <n> \n", prog_name);
   fprintf(stderr, "   n is the number of terms and should be >= 1\n");
   exit(0);
}  

void Get_args(int argc, char* argv[]) {
   if (argc != 3) Usage(argv[0]);
   thread_count = strtol(argv[1], NULL, 10);  
   if (thread_count <= 0 || thread_count > MAX_THREADS) Usage(argv[0]);
   n = strtoll(argv[2], NULL, 10);
   if (n <= 0) Usage(argv[0]);
}  


void* Trapezoidal_Rule(void* rank) {

  long long  integration=0 , stepSize, k;
  long subInterval = (long) rank;
  int i;
  stepSize = (1 - 0)/subInterval;
  integration = f(0) + f(1);
  
  for(i=1; i<= subInterval-1; i++)
 {
  k = 0 + i*stepSize;
  integration = integration + 2 * (f(k));
 }
  pthread_mutex_lock(&mutex);
  integration = integration * stepSize/2;
  pthread_mutex_unlock(&mutex);

  return NULL;
}



int main(int argc, char* argv[]) {
  long       thread;  
  pthread_t* thread_handles;
  
  
  
  Get_args(argc, argv);
  
  thread_handles = (pthread_t *) malloc(thread_count*sizeof(pthread_t));
  
  
  
  for (thread = 0; thread < thread_count; thread++)
    pthread_create(&thread_handles[thread], NULL, Trapezoidal_Rule, (void*)thread);
  
  for (thread = 0; thread < thread_count; thread++)
    pthread_join(thread_handles[thread], NULL);
  
  
  
  
  
  printf("   integration is = %.15f \n", sum);

  
  pthread_mutex_destroy(&mutex);
  free(thread_handles);
  return 0;
} 

Barmar
  • 741,623
  • 53
  • 500
  • 612
  • 1
    Division by zero maybe? What does your debugger say? – HolyBlackCat Sep 23 '21 at 19:56
  • `strtol` and `strtoll` could have failed and you didn't check. – user4581301 Sep 23 '21 at 19:57
  • 1
    Use a debugger to find out which line the error happens on. – Barmar Sep 23 '21 at 19:58
  • 1
    As always: run it in the debugger. That's the tool that will stop your program for you at the point where the problem manifested itself. – Kuba hasn't forgotten Monica Sep 23 '21 at 19:58
  • 5
    `stepSize = (1 - 0)/subInterval;` Is integer division. So `1` divided be any number other than `1` will be `0`. – 001 Sep 23 '21 at 20:05
  • Also want to use floats in f(x) – stark Sep 23 '21 at 20:16
  • 1
    Note that an integer divide-by-0 causes a floating point exception. This is just a prank by the C implementors. – stark Sep 23 '21 at 20:18
  • @Barmar OP indicates using C++ which is verified with the `g++ ...` build line, shouldn't C tag be removed instead? Certainly looks like C, however. – yano Sep 23 '21 at 20:21
  • Add `printf("subInterval = %ld\n", subInterval); fflush(stdout);` before `stepSize = (1 - 0)/subInterval;` – Ted Lyngmo Sep 23 '21 at 20:22
  • 1
    @yano That's so hard to read I didn't see it. But the code sure looks like C. – Barmar Sep 23 '21 at 20:24
  • @stark: [Fault likely lies with the hardware engineers, not C implementors.](https://stackoverflow.com/questions/60127747/why-was-sigfpe-used-for-integer-arithmetic-exceptions) – Eric Postpischil Sep 23 '21 at 20:24
  • I used printf("subInterval = %ld\n", subInterval); fflush(stdout); it gives subInterval = 0 subInterval = 1 Floating point exception (core dumped) @TedLyngmo – Reem Hasan Sep 23 '21 at 20:26
  • @ReemHasan `subInterval = 0` is what gives that FPE. You can't divide by zero. – Ted Lyngmo Sep 23 '21 at 20:29
  • okey any thought to prevent that @TedLyngmo – Reem Hasan Sep 23 '21 at 20:30
  • @ReemHasan You have `void* rank` and then you do `long subInterval = (long) rank;`, That is, you cast a `void*` to a `long` and I have no idea how to save that - oh, now I see, you use the `void*` argument to the thread... Why all these C-isms in a C++ program? – Ted Lyngmo Sep 23 '21 at 20:34
  • Changing it to `double subInterval = (long) rank;` would make it less likely to crash but I have no idea about what the program is supposed to do so I can't say if that's enough. I guess `k`, `stepSize` and `integration` would have to be `double`s too. – Ted Lyngmo Sep 23 '21 at 20:42
  • i used `if (subInterval != 0)` before `stepSize = (1 - 0)/subInterval; ` the error gone but this is the output ,,,,,,,,./output 4 9 subInterval = 0 subInterval = 1 subInterval = 3 subInterval = 2 integration is = 0.000000000000000 @TedLyngmo – Reem Hasan Sep 23 '21 at 20:50
  • You can't just make changes on a whim though. Use the `double` type for things that should be able to hold non-integer values. Adding an `if(subInterval != 0)` seems like band-aim if the rest of the formula is solid (and the correct data types are used) – Ted Lyngmo Sep 23 '21 at 20:56
  • `(void*)thread` may lose information. Better to pass the address of a `long`. – chux - Reinstate Monica Sep 23 '21 at 21:00
  • I also suggest that you start using the C++ classes and functions. This big program currently passes through when compiled as a `C` program. Use `std::thread` and `std::mutex` etc. Use `std::vector` as a base for al the storage that you now allocate manually with `malloc`. – Ted Lyngmo Sep 23 '21 at 21:07
  • You lock the mutex before `integration = integration * stepSize / 2;` but all those variables are local to thread. There is no need to lock there. Did you mean to update `sum` at that stage? You don't update `sum` anywhere right now. – Ted Lyngmo Sep 23 '21 at 21:20

1 Answers1

2

Floating point exception: core dumps -- nearly 100% of the time it's divide by zero. I don't think I've ever hit this for any other reason.

Joseph Larson
  • 8,530
  • 1
  • 19
  • 36