hello guys I'm trying to write Trapezoidal Rule in
parallel
usingC++
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;
}