parallelloop1.lpr 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. { Example for a parallel loop with MTProcs.
  2. Copyright (C) 2009 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 ParallelLoop1;
  26. {$mode objfpc}{$H+}
  27. uses
  28. {$IFDEF UNIX}
  29. cthreads, cmem,
  30. {$ENDIF}
  31. Classes, SysUtils, MTProcs;
  32. type
  33. TFindBestData = record
  34. List: TList;
  35. Value: Pointer;
  36. BlockCount: integer;
  37. Results: array of integer;
  38. end;
  39. PFindBestData = ^TFindBestData;
  40. procedure FindBestParallel(Index: PtrInt; Data: Pointer; Item: TMultiThreadProcItem);
  41. var
  42. i: integer;
  43. begin
  44. with PFindBestData(Data)^ do begin
  45. Results[Index]:=-1;
  46. i:=Index;
  47. while i<List.Count-1 do begin
  48. if List[i]=Value then // hier wuerde die teure Vergleichsoperation stehen
  49. Results[Index]:=i;
  50. inc(i,BlockCount);
  51. end;
  52. end;
  53. end;
  54. function FindBest(aList: TList; aValue: Pointer): integer;
  55. var
  56. Index: integer;
  57. Data: TFindBestData;
  58. begin
  59. with Data do begin
  60. List:=aList;
  61. Value:=aValue;
  62. BlockCount:=ProcThreadPool.MaxThreadCount;
  63. SetLength(Results,BlockCount);
  64. ProcThreadPool.DoParallel(@FindBestParallel,0,BlockCount-1,@Data);
  65. // Ergebnisse zusammenfassen
  66. Result:=-1;
  67. for Index:=0 to BlockCount-1 do
  68. if Results[Index]>=0 then
  69. Result:=Results[Index];
  70. end;
  71. end;
  72. function FindBest1(List: TList; Value: Pointer): integer;
  73. var
  74. i: integer;
  75. begin
  76. Result:=-1;
  77. i:=0;
  78. while i<List.Count do begin
  79. if List[i]=Value then // hier wuerde die teure Vergleichsoperation stehen
  80. Result:=i;
  81. inc(i);
  82. end;
  83. end;
  84. var
  85. List: TList;
  86. i: Integer;
  87. begin
  88. List:=TList.Create;
  89. for i:=0 to 100000000 do
  90. List.Add(Pointer(i));
  91. i:=FindBest(List,Pointer(9999));
  92. //i:=FindBest1(List,Pointer(9999));
  93. writeln('i=',i);
  94. end.