-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathrandom_mpi.cpp
1080 lines (1013 loc) · 22.3 KB
/
random_mpi.cpp
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
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# include <cmath>
# include <cstdlib>
# include <ctime>
# include <fstream>
# include <iomanip>
# include <iostream>
# include <mpi.h>
using namespace std;
int main ( int argc, char *argv[] );
int congruence ( int a, int b, int c, int *error );
int i4_gcd ( int i, int j );
int i4_max ( int i1, int i2 );
int i4_min ( int i1, int i2 );
int i4_sign ( int i );
void lcrg_anbn ( int a, int b, int c, int n, int *an, int *bn );
int lcrg_evaluate ( int a, int b, int c, int x );
int power_mod ( int a, int n, int m );
void timestamp ( );
//****************************************************************************80
int main ( int argc, char *argv[] )
//****************************************************************************80
//
// Purpose:
//
// MAIN is the main program for RANDOM_MPI.
//
// Discussion:
//
// This program demonstrates how P processors can generate the same
// sequence of random numbers as 1 processor.
//
// Licensing:
//
// This code is distributed under the GNU LGPL license.
//
// Modified:
//
// 16 June 2016
//
// Author:
//
// John Burkardt
//
// Reference:
//
// William Gropp, Ewing Lusk, Anthony Skjellum,
// Using MPI: Portable Parallel Programming with the
// Message-Passing Interface,
// Second Edition,
// MIT Press, 1999,
// ISBN: 0262571323,
// LC: QA76.642.G76.
//
{
int a;
int an;
int b;
int bn;
int c;
int error;
int id;
int j;
int k;
int k_hi;
int p;
int u;
int v;
//
// Initialize MPI.
//
MPI_Init ( &argc, &argv );
//
// Get the number of processors.
//
MPI_Comm_size ( MPI_COMM_WORLD, &p );
//
// Get the rank of this processor.
//
MPI_Comm_rank ( MPI_COMM_WORLD, &id );
//
// Print a message.
//
if ( id == 0 )
{
timestamp ( );
cout << "\n";
cout << "RANDOM_MPI - Master process:\n";
cout << " C++ version\n";
cout << " The number of processors is P = " << p << "\n";
cout << "\n";
cout << " This program shows how a stream of random numbers\n";
cout << " can be computed 'in parallel' in an MPI program.\n";
cout << "\n";
cout << " We assume we are using a linear congruential\n";
cout << " random number generator or LCRG, which takes\n";
cout << " an integer input and returns a new integer output:\n";
cout << "\n";
cout << " U = ( A * V + B ) mod C\n";
cout << "\n";
cout << " We assume that we want the MPI program to produce\n";
cout << " the same sequence of random values as a sequential\n";
cout << " program would - but we want each processor to compute\n";
cout << " one part of that sequence.\n";
cout << "\n";
cout << " We do this by computing a new LCRG which can compute\n";
cout << " every P'th entry of the original one.\n";
cout << "\n";
cout << " Our LCRG works with integers, but it is easy to\n";
cout << " turn each integer into a real number between [0,1].\n";
}
//
// A, B and C define the linear congruential random number generator.
//
a = 16807;
b = 0;
c = 2147483647;
if ( id == 0 )
{
cout << "\n";
cout << " LCRG parameters:\n";
cout << "\n";
cout << " A = " << a << "\n";
cout << " B = " << b << "\n";
cout << " C = " << c << "\n";
}
k_hi = p * 10;
//
// Processor 0 generates 10 * P random values.
//
if ( id == 0 )
{
cout << "\n";
cout << " Let processor 0 generate the entire random number sequence.\n";
cout << "\n";
cout << " K ID Input Output\n";
cout << "\n";
k = 0;
v = 12345;
cout << " " << setw(4) << k
<< " " << setw(4) << id
<< " " << " "
<< " " << setw(12) << v << "\n";
for ( k = 1; k <= k_hi; k++ )
{
u = v;
v = lcrg_evaluate ( a, b, c, u );
cout << " " << setw(4) << k
<< " " << setw(4) << id
<< " " << setw(12) << u
<< " " << setw(12) << v << "\n";
}
}
//
// Processor P now participates by computing the P-th part of the sequence.
//
lcrg_anbn ( a, b, c, p, &an, &bn );
if ( id == 0 )
{
cout << "\n";
cout << " LCRG parameters for P processors:\n";
cout << "\n";
cout << " AN = " << an << "\n";
cout << " BN = " << bn << "\n";
cout << " C = " << c << "\n";
cout << "\n";
cout << " Have ALL the processors participate in computing\n";
cout << " the same random number sequence.\n";
cout << "\n";
cout << " K ID Input Output\n";
cout << "\n";
}
//
// On System X, just to try to keep the output of the various processors
// sorted, I had to resort to a SLEEP command to keep the processors from
// competing. Obviously, this is only to make the output readable
// so we can verify it. It's NOT what you want to do in a production
// situation!
//
// sleep ( id * 2 );
//
// Use the basis LCRG to get the ID-th value in the sequence.
//
v = 12345;
for ( j = 1; j <= id; j++ )
{
u = v;
v = lcrg_evaluate ( a, b, c, u );
}
k = id;
cout << " " << setw(4) << k
<< " " << setw(4) << id
<< " " << " "
<< " " << setw(12) << v << "\n";
//
// Now use the "skipping" LCRG to compute the values with indices
// ID, ID+P, ID+2P, ...,
//
for ( k = id + p; k <= k_hi; k = k + p )
{
u = v;
v = lcrg_evaluate ( an, bn, c, u );
cout << " " << setw(4) << k
<< " " << setw(4) << id
<< " " << setw(12) << u
<< " " << setw(12) << v << "\n";
}
//
// Terminate MPI.
//
MPI_Finalize ( );
//
// Terminate.
//
if ( id == 0 )
{
cout << "\n";
cout << "RANDOM_MPI:\n";
cout << " Normal end of execution.\n";
cout << "\n";
timestamp ( );
}
return 0;
}
//****************************************************************************80
int congruence ( int a, int b, int c, bool *error )
//****************************************************************************80
//
// Purpose:
//
// CONGRUENCE solves a congruence of the form ( A * X = C ) mod B.
//
// Discussion:
//
// A, B and C are given integers. The equation is solvable if and only
// if the greatest common divisor of A and B also divides C.
//
// Licensing:
//
// This code is distributed under the GNU LGPL license.
//
// Modified:
//
// 22 May 2008
//
// Author:
//
// John Burkardt
//
// Reference:
//
// Eric Weisstein, editor,
// CRC Concise Encylopedia of Mathematics,
// CRC Press, 2002,
// Second edition,
// ISBN: 1584883472,
// LC: QA5.W45.
//
// Parameters:
//
// Input, int A, B, C, the coefficients of the Diophantine equation.
//
// Output, bool *ERROR, error flag, is TRUE if an error occurred..
//
// Output, int CONGRUENCE, the solution of the Diophantine equation.
// X will be between 0 and B-1.
//
{
# define N_MAX 100
int a_copy;
int a_mag;
int a_sign;
int b_copy;
int b_mag;
int b_sign;
int c_copy;
int g;
int k;
int n;
float norm_new;
float norm_old;
int q[N_MAX];
bool swap;
int temp;
int x;
int xnew;
int y;
int ynew;
int z;
//
// Defaults for output parameters.
//
*error = false;
x = 0;
y = 0;
//
// Special cases.
//
if ( a == 0 && b == 0 && c == 0 )
{
x = 0;
return x;
}
else if ( a == 0 && b == 0 && c != 0 )
{
*error = true;
x = 0;
return x;
}
else if ( a == 0 && b != 0 && c == 0 )
{
x = 0;
return x;
}
else if ( a == 0 && b != 0 && c != 0 )
{
x = 0;
if ( ( c % b ) != 0 )
{
*error = true;
}
return x;
}
else if ( a != 0 && b == 0 && c == 0 )
{
x = 0;
return x;
}
else if ( a != 0 && b == 0 && c != 0 )
{
x = c / a;
if ( ( c % a ) != 0 )
{
*error = true;
}
return x;
}
else if ( a != 0 && b != 0 && c == 0 )
{
// g = i4_gcd ( a, b );
// x = b / g;
x = 0;
return x;
}
//
// Now handle the "general" case: A, B and C are nonzero.
//
// Step 1: Compute the GCD of A and B, which must also divide C.
//
g = i4_gcd ( a, b );
if ( ( c % g ) != 0 )
{
*error = true;
return x;
}
a_copy = a / g;
b_copy = b / g;
c_copy = c / g;
//
// Step 2: Split A and B into sign and magnitude.
//
a_mag = abs ( a_copy );
a_sign = i4_sign ( a_copy );
b_mag = abs ( b_copy );
b_sign = i4_sign ( b_copy );
//
// Another special case, A_MAG = 1 or B_MAG = 1.
//
if ( a_mag == 1 )
{
x = a_sign * c_copy;
return x;
}
else if ( b_mag == 1 )
{
x = 0;
return x;
}
//
// Step 3: Produce the Euclidean remainder sequence.
//
if ( b_mag <= a_mag )
{
swap = false;
q[0] = a_mag;
q[1] = b_mag;
}
else
{
swap = true;
q[0] = b_mag;
q[1] = a_mag;
}
n = 3;
for ( ; ; )
{
q[n-1] = ( q[n-3] % q[n-2] );
if ( q[n-1] == 1 )
{
break;
}
n = n + 1;
if ( N_MAX < n )
{
*error = true;
cout << "\n";
cout << "CONGRUENCE - Fatal error!\n";
cout << " Exceeded number of iterations.\n";
exit ( 1 );
}
}
//
// Step 4: Now go backwards to solve X * A_MAG + Y * B_MAG = 1.
//
y = 0;
for ( k = n; 2 <= k; k-- )
{
x = y;
y = ( 1 - x * q[k-2] ) / q[k-1];
}
//
// Step 5: Undo the swapping.
//
if ( swap )
{
z = x;
x = y;
y = z;
}
//
// Step 6: Now apply signs to X and Y so that X * A + Y * B = 1.
//
x = x * a_sign;
//
// Step 7: Multiply by C, so that X * A + Y * B = C.
//
x = x * c_copy;
//
// Step 8: Now force 0 <= X < B.
//
x = x % b;
//
// Step 9: Force positivity.
//
if ( x < 0 )
{
x = x + b;
}
return x;
# undef N_MAX
}
//****************************************************************************80
int i4_gcd ( int i, int j )
//****************************************************************************80
//
// Purpose:
//
// I4_GCD finds the greatest common divisor of I and J.
//
// Discussion:
//
// Only the absolute values of I and J are considered, so that the
// result is always nonnegative.
//
// If I or J is 0, I4_GCD is returned as max ( 1, abs ( I ), abs ( J ) ).
//
// If I and J have no common factor, I4_GCD is returned as 1.
//
// Otherwise, using the Euclidean algorithm, I4_GCD is the
// largest common factor of I and J.
//
// Licensing:
//
// This code is distributed under the GNU LGPL license.
//
// Modified:
//
// 07 May 2003
//
// Author:
//
// John Burkardt
//
// Parameters:
//
// Input, int I, J, two numbers whose greatest common divisor
// is desired.
//
// Output, int I4_GCD, the greatest common divisor of I and J.
//
{
int ip;
int iq;
int ir;
//
// Return immediately if either I or J is zero.
//
if ( i == 0 )
{
return i4_max ( 1, abs ( j ) );
}
else if ( j == 0 )
{
return i4_max ( 1, abs ( i ) );
}
//
// Set IP to the larger of I and J, IQ to the smaller.
// This way, we can alter IP and IQ as we go.
//
ip = i4_max ( abs ( i ), abs ( j ) );
iq = i4_min ( abs ( i ), abs ( j ) );
//
// Carry out the Euclidean algorithm.
//
for ( ; ; )
{
ir = ip % iq;
if ( ir == 0 )
{
break;
}
ip = iq;
iq = ir;
}
return iq;
}
//****************************************************************************80
int i4_max ( int i1, int i2 )
//****************************************************************************80
//
// Purpose:
//
// I4_MAX returns the maximum of two I4's.
//
// Licensing:
//
// This code is distributed under the GNU LGPL license.
//
// Modified:
//
// 05 May 2003
//
// Author:
//
// John Burkardt
//
// Parameters:
//
// Input, int I1, I2, two integers to be compared.
//
// Output, int I4_MAX, the larger of I1 and I2.
//
{
int value;
if ( i2 < i1 )
{
value = i1;
}
else
{
value = i2;
}
return value;
}
//****************************************************************************80
int i4_min ( int i1, int i2 )
//****************************************************************************80
//
// Purpose:
//
// I4_MIN returns the smaller of two I4's.
//
// Licensing:
//
// This code is distributed under the GNU LGPL license.
//
// Modified:
//
// 05 May 2003
//
// Author:
//
// John Burkardt
//
// Parameters:
//
// Input, int I1, I2, two integers to be compared.
//
// Output, int I4_MIN, the smaller of I1 and I2.
//
{
int value;
if ( i1 < i2 )
{
value = i1;
}
else
{
value = i2;
}
return value;
}
//****************************************************************************80
int i4_sign ( int i )
//****************************************************************************80
//
// Purpose:
//
// I4_SIGN returns the sign of an I4.
//
// Discussion:
//
// The sign of 0 and all positive integers is taken to be +1.
// The sign of all negative integers is -1.
//
// Licensing:
//
// This code is distributed under the GNU LGPL license.
//
// Modified:
//
// 06 May 2003
//
// Author:
//
// John Burkardt
//
// Parameters:
//
// Input, int I, the integer whose sign is desired.
//
// Output, int I4_SIGN, the sign of I.
{
int value;
if ( i < 0 )
{
value = -1;
}
else
{
value = 1;
}
return value;
}
//****************************************************************************80
void lcrg_anbn ( int a, int b, int c, int n, int *an, int *bn )
//****************************************************************************80
//
// Purpose:
//
// LCRG_ANBN computes the "N-th power" of a linear congruential generator.
//
// Discussion:
//
// We are considering a linear congruential random number generator.
// The LCRG takes as input an integer value called SEED, and returns
// an updated value of SEED,
//
// SEED(out) = ( a * SEED(in) + b ) mod c.
//
// and an associated pseudorandom real value
//
// U = SEED(out) / c.
//
// In most cases, a user is content to call the LCRG repeatedly, with
// the updating of SEED being taken care of automatically.
//
// The purpose of this routine is to determine the values of AN and BN
// that describe the LCRG that is equivalent to N applications of the
// original LCRG.
//
// One use for such a facility would be to do random number computations
// in parallel. If each of N processors is to compute many random values,
// you can guarantee that they work with distinct random values
// by starting with a single value of SEED, using the original LCRG to generate
// the first N-1 "iterates" of SEED, so that you now have N "seed" values,
// and from now on, applying the N-th power of the LCRG to the seeds.
//
// If the K-th processor starts from the K-th seed, it will essentially
// be computing every N-th entry of the original random number sequence,
// offset by K. Thus the individual processors will be using a random
// number stream as good as the original one, and without repeating, and
// without having to communicate.
//
// To evaluate the N-th value of SEED directly, we start by ignoring
// the modular arithmetic, and working out the sequence of calculations
// as follows:
//
// SEED(0) = SEED.
// SEED(1) = a * SEED + b
// SEED(2) = a * SEED(1) + b = a^2 * SEED + a * b + b
// SEED(3) = a * SEED(2) + b = a^3 * SEED + a^2 * b + a * b + b
// ...
// SEED(N-1) = a * SEED(N-2) + b
//
// SEED(N) = a * SEED(N-1) + b = a^N * SEED
// + ( a^(n-1) + a^(n-2) + ... + a + 1 ) * b
//
// or, using the geometric series,
//
// SEED(N) = a^N * SEED + ( a^N - 1) / ( a - 1 ) * b
// = AN * SEED + BN
//
// Thus, from any SEED, we can determine the result of N applications of the
// original LCRG directly if we can solve
//
// ( a - 1 ) * BN = ( a^N - 1 ) * b in modular arithmetic,
//
// and evaluate:
//
// AN = a^N
//
// Licensing:
//
// This code is distributed under the GNU LGPL license.
//
// Modified:
//
// 23 April 2008
//
// Author:
//
// John Burkardt
//
// Reference:
//
// Barry Wilkinson, Michael Allen,
// Parallel Programming:
// Techniques and Applications Using Networked Workstations and Parallel Computers,
// Prentice Hall,
// ISBN: 0-13-140563-2,
// LC: QA76.642.W54.
//
// Parameters:
//
// Input, int A, the multiplier for the LCRG.
//
// Input, int B, the added value for the LCRG.
//
// Input, int C, the base for the modular arithmetic.
// For 32 bit arithmetic, this is often 2^31 - 1, or 2147483647. It is
// required that 0 < C.
//
// Input, int N, the "index", or number of times that the
// LCRG is to be applied. It is required that 0 <= N.
//
// Output, int *AN, *BN, the multiplier and added value for
// the LCRG that represent N applications of the original LCRG.
//
{
int am1;
int anm1tb;
bool ierror;
if ( n < 0 )
{
cerr << "\n";
cerr << "LCRG_ANBN - Fatal error!\n";
cerr << " Illegal input value of N = " << n << "\n";
exit ( 1 );
}
if ( c <= 0 )
{
cerr << "\n";
cerr << "LCRG_ANBN - Fatal error!\n";
cerr << " Illegal input value of C = " << c << "\n";
exit ( 1 );
}
if ( n == 0 )
{
*an = 1;
*bn = 0;
}
else if ( n == 1 )
{
*an = a;
*bn = b;
}
else
{
//
// Compute A^N.
//
*an = power_mod ( a, n, c );
//
// Solve
// ( a - 1 ) * BN = ( a^N - 1 ) mod B
// for BN.
//
am1 = a - 1;
anm1tb = ( *an - 1 ) * b;
*bn = congruence ( am1, c, anm1tb, &ierror );
if ( ierror )
{
cerr << "\n";
cerr << "LCRG_ANBN - Fatal error!\n";
cerr << " An error occurred in the CONGRUENCE routine.\n";
exit ( 1 );
}
}
return;
}
//****************************************************************************80
int lcrg_evaluate ( int a, int b, int c, int x )
//****************************************************************************80
//
// Purpose:
//
// LCRG_EVALUATE evaluates an LCRG, y = ( A * x + B ) mod C.
//
// Discussion:
//
// This routine cannot be recommended for production use. Because we want
// to do modular arithmetic, but the base is not a power of 2, we need to
// use "double precision" integers to keep accuracy.
//
// If we knew the base C, we could try to avoid overflow while not changing
// precision.
//
// If the base C was a power of 2, we could rely on the usual properties of
// integer arithmetic on computers, in which overflow bits, which are always
// ignored, don ot actually matter.
//
// Licensing:
//
// This code is distributed under the GNU LGPL license.
//
// Modified:
//
// 23 April 2008
//
// Author:
//
// John Burkardt
//
// Parameters:
//
// Input, int A, the multiplier for the LCRG.
//
// Input, int B, the added value for the LCRG.
//
// Input, int C, the base for the modular arithmetic.
// For 32 bit arithmetic, this is often 2^31 - 1, or 2147483647. It is
// required that 0 < C.
//
// Input, int X, the value to be processed.
//
// Output, int LCRG_EVALUATE, the processed value.
//
{
long long int a8;
long long int b8;
long long int c8;
long long int x8;
int y;
long long int y8;
//
// To avoid roundoff issues, we need to go to "double precision" integers.
// (Not available on all planets.)
//
a8 = ( long long int ) a;
b8 = ( long long int ) b;
c8 = ( long long int ) c;
x8 = ( long long int ) x;
y8 = ( a8 * x8 + b8 ) % c8;
y = ( int ) ( y8 );
if ( y < 0 )
{
y = y + c;
}
return y;
}
//****************************************************************************80
int power_mod ( int a, int n, int m )
//****************************************************************************80
//
// Purpose:
//
// POWER_MOD computes ( A^N ) mod M.
//
// Discussion:
//
// Some programming tricks are used to speed up the computation, and to
// allow computations in which A^N is much too large to store in a
// real word.
//
// First, for efficiency, the power A**N is computed by determining
// the binary expansion of N, then computing A, A^2, A^4, and so on
// by repeated squaring, and multiplying only those factors that
// contribute to A^N.
//
// Secondly, the intermediate products are immediately "mod'ed", which
// keeps them small.
//
// For instance, to compute mod ( A**13, 11 ), we essentially compute
//
// 13 = 1 + 4 + 8
//
// A^13 = A * A^4 * A^8
//
// A^13 mod ( 11 ) = A mod ( 11 ) * A^4 mod ( 11 ) * A^8 mod ( 11 ).
//
// Fermat's little theorem says that if P is prime, and A is not divisible
// by P, then ( A^(P-1) - 1 ) is divisible by P.
//
// Licensing:
//
// This code is distributed under the GNU LGPL license.
//
// Modified:
//
// 12 May 2007
//
// Author:
//
// John Burkardt
//
// Parameters:
//
// Input, int A, the base of the expression to be tested.
// A should be nonnegative.
//
// Input, int N, the power to which the base is raised.
// N should be nonnegative.
//
// Input, int M, the divisor against which the expression is tested.
// M should be positive.
//
// Output, int POWER_MOD, the remainder when A^N is divided by M.
//
{
long long int a_square2;
int d;
long long int m2;
int x;
long long int x2;
if ( a < 0 )
{
return -1;
}
if ( m <= 0 )
{
return -1;
}
if ( n < 0 )
{
return -1;