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); } 
  • 1. Do you have it compiled? for climbed on the line with #pragma . 2. You need to find the number of processors (even if it is N ), divide the set into tab values ​​into N 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, use N files, then merge together.) 3. The task is so simple that you can do without OpenMP. - VladD
  • @Vlad, as far as I remember the OMP syntax, there is a construct with for. I'm not sure that fprintf is a thread-safe function, and that it can be written to a file at the same time. - Arkady
  • @smallFish: write here like this: #pragma omp for for (int n = 0; n <10; ++ n) {// ... --- I also strongly doubt that fprintf thread-safe. - VladD
  • one
    There you should write: #pragma omp for for (i = ...) {...} and compile, for example, gcc -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

1 answer 1

Obviously, the task is to find exactly the sequence, and not the jumble that the parallelized for will produce. Write the results in an array (take the index from for), and then write the array in the master stream or outside the parallel region to a file.