parallelloop_nested1.lpr 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. { Example for a parallel loop with MTProcs.
  2. Copyright (C) 2017 Mattias Gaertner [email protected]
  3. This library is free software; you can redistribute it and/or modify it
  4. under the terms of the GNU Library General Public License as published by
  5. the Free Software Foundation; either version 2 of the License, or (at your
  6. option) any later version with the following modification:
  7. As a special exception, the copyright holders of this library give you
  8. permission to link this library with independent modules to produce an
  9. executable, regardless of the license terms of these independent modules,and
  10. to copy and distribute the resulting executable under terms of your choice,
  11. provided that you also meet, for each linked independent module, the terms
  12. and conditions of the license of that module. An independent module is a
  13. module which is not derived from or based on this library. If you modify
  14. this library, you may extend this exception to your version of the library,
  15. but you are not obligated to do so. If you do not wish to do so, delete this
  16. exception statement from your version.
  17. This program is distributed in the hope that it will be useful, but WITHOUT
  18. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  19. FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License
  20. for more details.
  21. You should have received a copy of the GNU Library General Public License
  22. along with this library; if not, write to the Free Software Foundation,
  23. Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  24. }
  25. program parallelloop_nested1;
  26. {$mode objfpc}{$H+}
  27. {$ModeSwitch nestedprocvars}
  28. uses
  29. {$IFDEF UNIX}
  30. cthreads, cmem,
  31. {$ENDIF}
  32. Classes, SysUtils, Math, MTProcs;
  33. function FindBestParallel(aList: TList; aValue: Pointer): integer;
  34. var
  35. BlockSize: PtrInt;
  36. Results: array of integer;
  37. procedure InParallel(Index: PtrInt; Data: Pointer; Item: TMultiThreadProcItem);
  38. var
  39. i, StartIndex, EndIndex: PtrInt;
  40. begin
  41. Results[Index]:=-1;
  42. StartIndex:=Index*BlockSize;
  43. EndIndex:=Min(StartIndex+BlockSize,aList.Count);
  44. //if MainThreadID=GetCurrentThreadId then
  45. // writeln('FindBestParallel Index=',Index,' StartIndex=',StartIndex,' EndIndex=',EndIndex);
  46. for i:=StartIndex to EndIndex-1 do begin
  47. if aList[i]=aValue then // imagine here an expensive compare function
  48. Results[Index]:=i;
  49. end;
  50. end;
  51. var
  52. Index: integer;
  53. BlockCount: PtrInt;
  54. begin
  55. ProcThreadPool.CalcBlockSize(aList.Count,BlockCount,BlockSize);
  56. SetLength(Results,BlockCount);
  57. //writeln('FindBestParallel BlockCount=',BlockCount,' List.Count=',aList.Count,' BlockSize=',BlockSize);
  58. ProcThreadPool.DoParallelNested(@InParallel,0,BlockCount-1);
  59. // collect results
  60. Result:=-1;
  61. for Index:=0 to BlockCount-1 do
  62. if Results[Index]>=0 then
  63. Result:=Results[Index];
  64. end;
  65. function FindBestSingleThreaded(List: TList; Value: Pointer): integer;
  66. var
  67. i: integer;
  68. begin
  69. Result:=-1;
  70. i:=0;
  71. while i<List.Count do begin
  72. if List[i]=Value then // imagine here an expensive compare function
  73. Result:=i;
  74. inc(i);
  75. end;
  76. end;
  77. var
  78. List: TList;
  79. i: Integer;
  80. begin
  81. List:=TList.Create;
  82. for i:=0 to 100000000 do
  83. List.Add(Pointer(i));
  84. writeln('searching ...');
  85. i:=FindBestParallel(List,Pointer(List.Count-2));
  86. writeln('parallel search i=',i);
  87. i:=FindBestSingleThreaded(List,Pointer(List.Count-2));
  88. writeln('linear search i=',i);
  89. end.