Natools

Check-in [1cdd0709b0]
Login
Overview
Comment:tools/smaz: use the new parallelization framework
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1: 1cdd0709b0e0851017f39ccd1080c5605b6d61f5
User & Date: nat on 2016-10-14 21:01:39
Other Links: manifest | tags
Context
2016-10-15
18:50
smaz-tools: new facility for faster hashing of dynamic dictionaries check-in: f651755fea user: nat tags: trunk
2016-10-14
21:01
tools/smaz: use the new parallelization framework check-in: 1cdd0709b0 user: nat tags: trunk
2016-10-13
21:47
parallelism: add a new framework with a task-local accumulator check-in: db5f3c6f57 user: nat tags: trunk
Changes

Modified tools/smaz.adb from [b920b70a9d] to [50cf344fab].

240
241
242
243
244
245
246
247

248
249
250
251
252
253



254
255


256
257



258
259
260
261

262
263






264
265
266

267
268


269
270
271



272
273
274



275
276
277



278
279
280
281
282
283



284
285
286
287
288
289

290
291
292
293
294
295

296
297
298

299
300
301
302
303
304
305
306
307

308
309
310


311
312
313
314
315
316
317
240
241
242
243
244
245
246

247

248
249
250
251

252
253
254
255

256
257
258

259
260
261
262
263
264

265
266

267
268
269
270
271
272
273
274

275
276

277
278
279


280
281
282



283
284
285
286
287

288
289
290
291
292
293



294
295
296
297
298
299
300
301

302
303
304
305
306
307

308
309
310

311
312
313
314
315





316



317
318
319
320
321
322
323
324
325







-
+
-




-
+
+
+

-
+
+

-
+
+
+



-
+

-
+
+
+
+
+
+


-
+

-
+
+

-
-
+
+
+
-
-
-
+
+
+


-
+
+
+



-
-
-
+
+
+





-
+





-
+


-
+




-
-
-
-
-
+
-
-
-
+
+







      Dict : in Natools.Smaz.Dictionary;
      Corpus : in Natools.Smaz.Tools.String_Lists.List;
      Compressed_Size : out Ada.Streams.Stream_Element_Count;
      Counts : out Natools.Smaz.Tools.Dictionary_Counts)
   is
      package String_Lists renames Natools.Smaz.Tools.String_Lists;

      type State is record
      type Result_Values is record
         Position : String_Lists.Cursor;
         Compressed_Size : Ada.Streams.Stream_Element_Count;
         Counts : Natools.Smaz.Tools.Dictionary_Counts;
      end record;

      procedure Initialize_Job
      procedure Initialize (Result : in out Result_Values);

      procedure Get_Next_Job
        (Global : in out String_Lists.Cursor;
         Job : out State);
         Job : out String_Lists.Cursor;
         Terminated : out Boolean);

      procedure Do_Job (Job : in out State);
      procedure Do_Job
        (Result : in out Result_Values;
         Job : in String_Lists.Cursor);

      procedure Gather_Result
        (Global : in out String_Lists.Cursor;
         Job : in State);
         Partial : in Result_Values);

      function Is_Finished (Global : in String_Lists.Cursor) return Boolean;

      procedure Initialize (Result : in out Result_Values) is
      begin
         Result := (Compressed_Size => 0,
                    Counts => (others => 0));
      end Initialize;


      procedure Initialize_Job
      procedure Get_Next_Job
        (Global : in out String_Lists.Cursor;
         Job : out State) is
         Job : out String_Lists.Cursor;
         Terminated : out Boolean) is
      begin
         Job := (Position => Global,
                 Compressed_Size => 0,
         Job := Global;
         Terminated := not String_Lists.Has_Element (Global);
         if not Terminated then
                 Counts => (others => 0));
         String_Lists.Next (Global);
      end Initialize_Job;
            String_Lists.Next (Global);
         end if;
      end Get_Next_Job;


      procedure Do_Job (Job : in out State) is
      procedure Do_Job
        (Result : in out Result_Values;
         Job : in String_Lists.Cursor) is
      begin
         Natools.Smaz.Tools.Evaluate_Dictionary_Partial
           (Dict,
            String_Lists.Element (Job.Position),
            Job.Compressed_Size,
            Job.Counts);
            String_Lists.Element (Job),
            Result.Compressed_Size,
            Result.Counts);
      end Do_Job;


      procedure Gather_Result
        (Global : in out String_Lists.Cursor;
         Job : in State)
         Partial : in Result_Values)
      is
         pragma Unreferenced (Global);
         use type Ada.Streams.Stream_Element_Count;
         use type Natools.Smaz.Tools.String_Count;
      begin
         Compressed_Size := Compressed_Size + Job.Compressed_Size;
         Compressed_Size := Compressed_Size + Partial.Compressed_Size;

         for I in Counts'Range loop
            Counts (I) := Counts (I) + Job.Counts (I);
            Counts (I) := Counts (I) + Partial.Counts (I);
         end loop;
      end Gather_Result;


      function Is_Finished (Global : in String_Lists.Cursor) return Boolean is
      begin
         return not String_Lists.Has_Element (Global);
      end Is_Finished;

      procedure Parallel_Run

      procedure Parallel_Run is new Natools.Parallelism.Single_Accumulator_Run
        (String_Lists.Cursor, State);
        is new Natools.Parallelism.Per_Task_Accumulator_Run
           (String_Lists.Cursor, Result_Values, String_Lists.Cursor);

      Cursor : String_Lists.Cursor := String_Lists.First (Corpus);
   begin
      Compressed_Size := 0;
      Counts := (others => 0);
      Parallel_Run (Cursor, Job_Count);
   end Parallel_Evaluate_Dictionary;