Hello! I am not sure I am in the right forum but there are very few documentation on this subject across the Internet.

I am implementing numerical integration method using the simple trapezoidal rule.

Due to the specific function including sin(f*t)df to speedup the process I am trying to implement parallel.for method but I can't understand how to count sums in each period.

Normal implementation of the method is:

```
function integrate2(a, b: real; n: integer): real;
var
i: integer;
res, h, x: real;
begin
res := 0;
h := (b - a) / n;
```**for** i := 1 to n - 1 **do** begin
x := a + i * h;
res := res + f(x);
end;
Result := h * ((f(a) + f(b)) / 2 + res);
end;

and now threaded version:

```
function integrate(a, b: real; n: integer): real;
var
i: integer;
res,pres, h, x:
```**double**;
begin
res := 0;
Tparallel.&**for**(1,n-1,procedure(i:integer)
begin
h := (b - a) / n;
x := a + i * h;
pres:=f(x);
// TInterlocked.Exchange(sum[i],pres);
res := res + f(x);
// TThread.Queue(nil,
// procedure
// begin
//
// //update the progress bar in the main thread
// //res := res + f(x);
// end );
end
);
Result := h * ((f(a) + f(b)) / 2 + res);
end;

As you can see above I've tried a lot of options I was able to find but nothing worked for me.

TInterlocked is working fine but only with Integers.

What is the proper way to collect and sum data from the parallel.sum loop ?

Connect with Us