Page Menu
Home
c4science
Search
Configure Global Search
Log In
Files
F100796582
integral.c
No One
Temporary
Actions
Download File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Subscribers
None
File Metadata
Details
File Info
Storage
Attached
Created
Sun, Feb 2, 20:33
Size
2 KB
Mime Type
text/x-c
Expires
Tue, Feb 4, 20:33 (1 d, 23 h)
Engine
blob
Format
Raw Data
Handle
24035999
Attached To
R10834 Project_multiproc
integral.c
View Options
/*
============================================================================
Filename : integral.c
Author : Your names goes here
SCIPER : Your SCIPER numbers
============================================================================
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "utility.h"
#include "function.c"
int verbose = 0;
int tablemode = 0;
double integrate (int num_threads, int samples, int a, int b, double (*f)(double));
int main (int argc, const char *argv[]) {
int num_threads, num_samples, a, b;
double integral;
if (argc < 5) {
printf("Invalid input! Usage: ./integral <num_threads> <num_samples> <a> <b>\n");
return 1;
} else {
num_threads = atoi(argv[1]);
num_samples = atoi(argv[2]);
a = atoi(argv[3]);
b = atoi(argv[4]);
for (int arg = 5; arg < argc; ++arg) {
if (!strcmp(argv[arg], "-v"))
verbose = 1; // set verbose mode
else if (!strcmp(argv[arg], "-t"))
tablemode = 1; // set table mode
}
}
set_clock();
/* You can use your self-defined funtions by replacing identity_f. */
integral = integrate (num_threads, num_samples, a, b, identity_f);
double ttime = elapsed_time(); // compute it once
if (tablemode)
printf("%d %d %d %.15g %.4g\n", num_threads, a, b, integral, ttime); // table output mode
else
printf("- Using %d threads: integral on [%d,%d] = %.15g computed in %.4gs.\n", num_threads, a, b, integral, ttime);
return 0;
}
double integrate (int num_threads, int samples, int a, int b, double (*f)(double)) {
double integral;
/* Your code goes here */
omp_set_num_threads(num_threads);
int tid;
integral = 0;
double sum;
#pragma omp parallel private(tid, sum) shared(integral, num_threads)
{
sum = 0;
tid = omp_get_thread_num();
rand_gen gen = init_rand();
for(int i = tid; i<samples; i+=num_threads)
{
double x = (next_rand(gen)*abs(b-a))+a;
sum += f(x);
}
#pragma omp atomic
integral += sum;
}
return integral*abs(b-a)/samples;
}
Event Timeline
Log In to Comment