phcpack_2.4.88_e448e94a/src/Ada/Tasking/ts_mtperm.adb

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
with Ada.Calendar;
with text_io;                            use text_io;
with Communications_with_User;           use Communications_with_User; 
with Timing_Package;                     use Timing_Package;
with Time_Stamps;
with Standard_Natural_Numbers;           use Standard_Natural_Numbers;
with Standard_Natural_Numbers_io;        use Standard_Natural_Numbers_io;
with Standard_Integer_Numbers;           use Standard_Integer_Numbers;
with Standard_Integer_Numbers_io;        use Standard_Integer_Numbers_io;
with Standard_Random_Numbers;
with Standard_Integer_Vectors;
with Standard_Integer_Vectors_io;        use Standard_Integer_Vectors_io;
with Standard_Integer_VecVecs;
with Standard_Integer_Matrices;
with Standard_Integer_Matrices_io;       use Standard_Integer_Matrices_io;
with Boolean_Matrices;
with Boolean_Matrices_io;                use Boolean_Matrices_io;
with Standard_Random_Matrices;
with Integer_Permanents;                 use Integer_Permanents;
with Multitasking_Integer_Permanents;

procedure ts_mtperm is

-- DESCRIPTION :
--   Interactive test on the permanent computation of an integer matrix.

  procedure Integer_Random_Test ( dim : in natural32 ) is

  -- DESCRIPTION :
  --   Generates a random integer matrix of the given dimension dim
  --   and computes its permanent.

    idm : constant integer32 := integer32(dim);
    mat : constant Standard_Integer_Matrices.Matrix(1..idm,1..idm)
        := Standard_Random_Matrices.Random_Matrix(dim,dim,0,9);
    per : integer64;
    ans : character;
    timer : Timing_Widget;

  begin
    put("A random "); put(dim,1); put("-by-"); put(dim,1);
    put_line(" matrix : "); put(mat);
    put("Do you want output of the factors ? (y/n) ");
    Ask_Yes_or_No(ans);
    tstart(timer);
    if ans = 'y' 
     then per := Permanent(standard_output,mat);
     else per := Permanent(mat);
    end if;
    tstop(timer);
    put("The permanent : "); put(per,1); new_line;
    new_line;
    print_times(standard_output,timer,"Permanent Computation");
  end Integer_Random_Test;

  procedure Boolean_Random_Test ( dim : in natural32 ) is

  -- DESCRIPTION :
  --   Generates a random Boolean matrix of the given dimension dim
  --   and computes its permanent.

    idm : constant integer32 := integer32(dim);
    mat : Boolean_Matrices.Matrix(1..idm,1..idm)
        := Standard_Random_Matrices.Random_Matrix(dim);
    per : integer64;
    ans : character;
    timer : Timing_Widget;

  begin
    put("A random "); put(dim,1); put("-by-"); put(dim,1);
    put_line(" matrix : "); put(mat);
    put("Do you want output of the factors ? (y/n) ");
    Ask_Yes_or_No(ans);
    tstart(timer);
    if ans = 'y' 
     then per := Permanent(standard_output,mat);
     else per := Permanent(mat);
    end if;
    tstop(timer);
    put("The permanent : "); put(per,1); new_line;
    new_line;
    print_times(standard_output,timer,"Permanent Computation");
  end Boolean_Random_Test;

  procedure Random_Test is

  -- DESCRIPTION :
  --   Prompts the user for the dimension
  --   and asks whether the matrix is Boolean or not.

    dim : natural32 := 0;
    ans : character;

  begin
    new_line;
    put("Give the dimension : "); get(dim);
    put("Generate a Boolean matrix ? (y/n) ");
    Ask_Yes_or_No(ans);
    if ans = 'y'
     then Boolean_Random_Test(dim);
     else Integer_Random_Test(dim);
    end if;
  end Random_Test;

  procedure Start_Integer_Random_Test ( dim : in natural32 ) is

  -- DESCRIPTION :
  --   Generates a random integer matrix of the given dimension dim
  --   and computes its permanent, starting with the column indices
  --   for a number of rows first.

    idm : constant integer32 := integer32(dim);
    mat : constant Standard_Integer_Matrices.Matrix(1..idm,1..idm)
        := Standard_Random_Matrices.Random_Matrix(dim,dim,0,9);
    nbr : integer32 := 0;

  begin
    put("A random "); put(dim,1); put("-by-"); put(dim,1);
    put_line(" matrix : "); put(mat);
    put("Give the number of rows : "); get(nbr);
    declare
      nbstc : constant integer32
            := Number_of_Start_Columns(integer32(dim),nbr);
      stcol : Standard_Integer_VecVecs.VecVec(1..nbstc);
      cols : Standard_Integer_Vectors.Vector(mat'range(1))
           := (mat'range(1) => 0);
      cnts : Standard_Integer_Vectors.Vector(mat'range(1))
           := (mat'range(1) => 1);
      idx : integer32 := 0;
      per : integer64;
      ans : character;
    begin
      Start_Columns(mat'first(1),nbr,mat,cols,cnts,idx,stcol);
      put_line("The start column indices : ");
      for k in 1..idx loop
        put(stcol(k)); new_line;
      end loop;
      put("Do you want output of the factors ? (y/n) ");
      Ask_Yes_or_No(ans);
      if ans = 'y'
       then per := Start_Permanent(standard_output,nbr+1,stcol(1..idx),mat);
       else per := Start_Permanent(nbr+1,stcol(1..idx),mat);
      end if;
      put("The permanent : "); put(per,1); new_line;
      per := Permanent(mat);
      put("The permanent : "); put(per,1); new_line;
    end;
  end Start_Integer_Random_Test;

  procedure Start_Boolean_Random_Test ( dim : in natural32 ) is

  -- DESCRIPTION :
  --   Generates a random Boolean matrix of the given dimension dim
  --   and computes its permanent, starting with the column indices
  --   for a number of rows first.

    idm : constant integer32 := integer32(dim);
    mat : Boolean_Matrices.Matrix(1..idm,1..idm)
        := Standard_Random_Matrices.Random_Matrix(dim);
    nbr : integer32 := 0;

  begin
    put("A random "); put(dim,1); put("-by-"); put(dim,1);
    put_line(" matrix : "); put(mat);
    put("Give the number of rows : "); get(nbr);
    declare
      nbstc : constant integer32
            := Number_of_Start_Columns(integer32(dim),nbr);
      stcol : Standard_Integer_VecVecs.VecVec(1..nbstc);
      cols : Standard_Integer_Vectors.Vector(mat'range(1))
           := (mat'range(1) => 0);
      cnts : Standard_Integer_Vectors.Vector(mat'range(1))
           := (mat'range(1) => 1);
      idx : integer32 := 0;
      per : integer64;
      ans : character;
    begin
      Start_Columns(mat'first(1),nbr,mat,cols,cnts,idx,stcol);
      put_line("The start column indices : ");
      for k in 1..idx loop
        put(stcol(k)); new_line;
      end loop;
      put("Do you want output of the factors ? (y/n) ");
      Ask_Yes_or_No(ans);
      if ans = 'y'
       then per := Start_Permanent(standard_output,nbr+1,stcol(1..idx),mat);
       else per := Start_Permanent(nbr+1,stcol(1..idx),mat);
      end if;
      put("The permanent : "); put(per,1); new_line;
      per := Permanent(mat);
      put("The permanent : "); put(per,1); new_line;
    end;
  end Start_Boolean_Random_Test;

  procedure Start_Random_Test is

  -- DESCRIPTION :
  --   Prompts the user for the dimension
  --   and asks whether the matrix is Boolean or not.

    dim : natural32 := 0;
    ans : character;

  begin
    new_line;
    put("Give the dimension : "); get(dim);
    put("Generate a Boolean matrix ? (y/n) ");
    Ask_Yes_or_No(ans);
    if ans = 'y'
     then Start_Boolean_Random_Test(dim);
     else Start_Integer_Random_Test(dim);
    end if;
  end Start_Random_Test;

  procedure Multitasked_Boolean_Test ( dim : in natural32 ) is

  -- DESCRIPTION :
  --   Generates a random integer matrix of the given dimension dim
  --   and computes its permanent.
  --   Elapsed wall clock times in seconds are written.

    idm : constant integer32 := integer32(dim);
    mat : constant Boolean_Matrices.Matrix(1..idm,1..idm)
        := Standard_Random_Matrices.Random_Matrix(dim);
    per : integer64;
    nbrows,ntasks : integer32 := 0;
    ans : character;
    verb : boolean;
    multstart,multstop,seristart,seristop : Ada.Calendar.Time;

  begin
    put("A random "); put(dim,1); put("-by-"); put(dim,1);
    put_line(" matrix : "); put(mat);
    put("Give the number of first rows : "); get(nbrows);
    put("Give the number of tasks : "); get(ntasks);
    put("Output during computations ? "); Ask_Yes_or_No(ans);
    verb := (ans = 'y');
    multstart := Ada.Calendar.Clock;
    per := Multitasking_Integer_Permanents.Permanent(mat,nbrows,ntasks,verb);
    multstop := Ada.Calendar.Clock;
    put("The permanent : "); put(per,1); new_line;
    seristart := Ada.Calendar.Clock;
    per := Permanent(mat);
    seristop := Ada.Calendar.Clock;
    put("The permanent : "); put(per,1); new_line;
    new_line;
    put_line("Elapsed time on multitasking : ");
    Time_Stamps.Write_Elapsed_Time(standard_output,multstart,multstop);
    new_line;
    put_line("Elapsed time without multitasking : ");
    Time_Stamps.Write_Elapsed_Time(standard_output,seristart,seristop);
  end Multitasked_Boolean_Test;

  procedure Multitasked_Integer_Test ( dim : in natural32 ) is

  -- DESCRIPTION :
  --   Generates a random integer matrix of the given dimension dim
  --   and computes its permanent.

    idm : constant integer32 := integer32(dim);
    mat : constant Standard_Integer_Matrices.Matrix(1..idm,1..idm)
        := Standard_Random_Matrices.Random_Matrix(dim,dim,0,9);
    per : integer64;
    nbrows,ntasks : integer32 := 0;
    ans : character;
    verb : boolean;
    multstart,multstop,seristart,seristop : Ada.Calendar.Time;

  begin
    put("A random "); put(dim,1); put("-by-"); put(dim,1);
    put_line(" matrix : "); put(mat);
    put("Give the number of first rows : "); get(nbrows);
    put("Give the number of tasks : "); get(ntasks);
    put("Output during computations ? "); Ask_Yes_or_No(ans);
    verb := (ans = 'y');
    multstart := Ada.Calendar.Clock;
    per := Multitasking_Integer_Permanents.Permanent(mat,nbrows,ntasks,verb);
    multstop := Ada.Calendar.Clock;
    put("The permanent : "); put(per,1); new_line;
    seristart := Ada.Calendar.Clock;
    per := Permanent(mat);
    seristop := Ada.Calendar.Clock;
    put("The permanent : "); put(per,1); new_line;
    new_line;
    put_line("Elapsed time on multitasking : ");
    Time_Stamps.Write_Elapsed_Time(standard_output,multstart,multstop);
    new_line;
    put_line("Elapsed time without multitasking : ");
    Time_Stamps.Write_Elapsed_Time(standard_output,seristart,seristop);
  end Multitasked_Integer_Test;

  procedure Multitasked_Test is

  -- DESCRIPTION :
  --   Prompts the user for the dimension
  --   and asks whether the matrix is Boolean or not.

    dim : natural32 := 0;
    ans : character;

  begin
    new_line;
    put("Give the dimension : "); get(dim);
    put("Generate a Boolean matrix ? (y/n) ");
    Ask_Yes_or_No(ans);
    if ans = 'y'
     then Multitasked_Boolean_Test(dim);
     else Multitasked_Integer_Test(dim);
    end if;
  end Multitasked_Test;

  procedure Doubling_Multitasked_Boolean_Test ( dim : in natural32 ) is

  -- DESCRIPTION :
  --   Generates a random integer matrix of the given dimension dim
  --   and computes its permanent.
  --   Elapsed wall clock times in seconds are written.

    idm : constant integer32 := integer32(dim);
    mat : constant Boolean_Matrices.Matrix(1..idm,1..idm)
        := Standard_Random_Matrices.Random_Matrix(dim);
    per : integer64;
    nbrows,ntasks,maxnbtasks : integer32 := 0;
    ans : character;
    verb : boolean;
    timestart,timestop : Ada.Calendar.Time;

  begin
    put("A random "); put(dim,1); put("-by-"); put(dim,1);
    put_line(" matrix : "); put(mat);
    put("Give the number of first rows : "); get(nbrows);
    put("Give the largest number of tasks : "); get(maxnbtasks);
    put("Output during computations ? "); Ask_Yes_or_No(ans);
    verb := (ans = 'y');
    timestart := Ada.Calendar.Clock;
    per := Permanent(mat);
    timestop := Ada.Calendar.Clock;
    put("The permanent : "); put(per,1); new_line;
    put_line("Elapsed time without multitasking : ");
    Time_Stamps.Write_Elapsed_Time(standard_output,timestart,timestop);
    ntasks := 2;
    while ntasks <= maxnbtasks loop
      timestart := Ada.Calendar.Clock;
      per := Multitasking_Integer_Permanents.Permanent(mat,nbrows,ntasks,verb);
      timestop := Ada.Calendar.Clock;
      put("The permanent : "); put(per,1); new_line;
      put("With "); put(ntasks,1); put(" tasks.  ");
      Time_Stamps.Write_Elapsed_Time(standard_output,timestart,timestop);
      ntasks := 2*ntasks;
    end loop;
  end Doubling_Multitasked_Boolean_Test;

  procedure Doubling_Multitasked_Integer_Test ( dim : in natural32 ) is

  -- DESCRIPTION :
  --   Generates a random integer matrix of the given dimension dim
  --   and computes its permanent.

    idm : constant integer32 := integer32(dim);
    mat : constant Standard_Integer_Matrices.Matrix(1..idm,1..idm)
        := Standard_Random_Matrices.Random_Matrix(dim,dim,0,9);
    per : integer64;
    nbrows,ntasks,maxnbtasks : integer32 := 0;
    ans : character;
    verb : boolean;
    timestart,timestop : Ada.Calendar.Time;

  begin
    put("A random "); put(dim,1); put("-by-"); put(dim,1);
    put_line(" matrix : "); put(mat);
    put("Give the number of first rows : "); get(nbrows);
    put("Give the maximum number of tasks : "); get(maxnbtasks);
    put("Output during computations ? "); Ask_Yes_or_No(ans);
    verb := (ans = 'y');
    timestart := Ada.Calendar.Clock;
    per := Permanent(mat);
    timestop := Ada.Calendar.Clock;
    put("The permanent : "); put(per,1); new_line;
    put_line("Elapsed time without multitasking : ");
    Time_Stamps.Write_Elapsed_Time(standard_output,timestart,timestop);
    ntasks := 2;
    while ntasks <= maxnbtasks loop
      timestart := Ada.Calendar.Clock;
      per := Multitasking_Integer_Permanents.Permanent(mat,nbrows,ntasks,verb);
      timestop := Ada.Calendar.Clock;
      put("The permanent : "); put(per,1); new_line;
      put("With "); put(ntasks,1); put(" tasks.  ");
      Time_Stamps.Write_Elapsed_Time(standard_output,timestart,timestop);
      ntasks := 2*ntasks;
    end loop;
  end Doubling_Multitasked_Integer_Test;

  procedure Doubling_Multitasked_Test is

  -- DESCRIPTION :
  --   Prompts the user for the dimension
  --   and asks whether the matrix is Boolean or not.
  --   Starting with the serial run,
  --   the number of tasks doubles in each stage.

    dim : natural32 := 0;
    ans : character;

  begin
    new_line;
    put("Give the dimension : "); get(dim);
    put("Generate a Boolean matrix ? (y/n) ");
    Ask_Yes_or_No(ans);
    if ans = 'y'
     then Doubling_Multitasked_Boolean_Test(dim);
     else Doubling_Multitasked_Integer_Test(dim);
    end if;
  end Doubling_Multitasked_Test;

  procedure Main is

    ans : character;

  begin
    new_line;
    put_line("MENU to test a permament computation : ");
    put_line("  1. test on a randomly generated matrix;");
    put_line("  2. compute start columns for the first rows;");
    put_line("  3. test multitasked permanent computation;");
    put_line("  4. double the number of tasks in each stage.");
    put("Type 1, 2, 3, or 4 to choose a test : ");
    Ask_Alternative(ans,"1234");
    case ans is
      when '1' => Random_Test;
      when '2' => Start_Random_Test;
      when '3' => Multitasked_Test;
      when '4' => Doubling_Multitasked_Test;
      when others => null;
    end case;
  end Main;

begin
  Main;
end ts_mtperm;