Created
July 14, 2018 20:57
-
-
Save brimonk/dcf975616770741a20e4583cb61b08fc to your computer and use it in GitHub Desktop.
Integral Experiment in C
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Brian Chrzanowski | |
* Fri Jul 13, 2018 13:42 | |
* | |
* perform integrals over any function that returns and takes a double | |
* compile : cc integral.c -lm | |
*/ | |
#include <stdio.h> | |
#include <math.h> | |
double integral(double(*f)(double x), double a, double b, int n); | |
double func_x_2(double x); | |
double func_x(double x); | |
int main(int argc, char **argv) | |
{ | |
printf("integral of cos %lf\n", integral(cos, 0, M_PI / 2, 1000)); | |
printf("integral of x^2 %lf\n", integral(func_x_2, 0, 4, 1000)); | |
printf("integral of x %lf\n", integral(func_x, 0, 4, 1000)); | |
return 0; | |
} | |
/* what would this integral function that uses CUDA look like? */ | |
double integral(double(*f)(double x), double a, double b, int n) | |
{ | |
int i; | |
double step = (b - a) / n; // width of each small rectangle | |
double area = 0.0; // signed area | |
for (i = 0; i < n; i++) { | |
area += f(a + (i + 0.5) * step) * step; // sum up each rect | |
} | |
return area; | |
} | |
void cuda_integral(double(*f)(double x), double *arr, double a, double b) | |
{ | |
/* | |
* get each index, etc | |
*/ | |
/* store each thread's output sum into arr*/ | |
/* | |
* use shifts to move all the sums from 0...N positions, | |
* to the 0th position, where the intermediate positions are garbage | |
*/ | |
} | |
double func_x_2(double x) | |
{ | |
return x * x; | |
} | |
double func_x(double x) | |
{ | |
return x; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment