Tabulation of the function 1 / (3rd degree root of x). In input.txt, spacing and pitch, in output.txt results. How can I parallelize the program as much as possible?
#include <stdio.h> double function (double x) { double a, b; a = 0; b = x; while (1>0) { a = 0.5*(b + 3*x / (2*b*b + x/b)); if(((ab)<=(0.1) && (ab)>=0) || ((ab)>(-0.01) && (ab)<0)) return a; b = a; //a = 0.5*(b + 3*x / (2*b*b + x/b)); //if (((ab)<(0.1) && (ab)>0) || ((ab)>(-0.1) && (ab)<0)) //return 1/a; //a = b; } } int main() { FILE *f; FILE *fo; f = fopen("input.txt", "rt"); fo = fopen("output.txt", "wt"); double a, b, step; double i; fscanf(f, "%lf", &a); fscanf(f, "%lf", &b); fscanf(f, "%lf", &step); #pragma omp for (i = a; i <= b; i += step) { fprintf(fo, "%.14f\n", function(i)); } fclose(f); fclose(fo); }
for
climbed on the line with#pragma
. 2. You need to find the number of processors (even if it isN
), divide the set into tab values intoN
parts, and calculate the values of each part in its thread. It is better to write to the file later when the calculations are completed. (If, of course, it fits in memory. If not, useN
files, then merge together.) 3. The task is so simple that you can do without OpenMP. - VladDfprintf
thread-safe. - VladDgcc -fopenmp ...
Without the -fopenmp key, gcc simply ignores#pragma omp
. As for thread-safe-whether fprintf () or not (and in what OS), this is another question. - avp