1 | /////////////////////////////////////////////////////////////////////////////// |
---|
2 | // File : pgcd.c |
---|
3 | // Date : November 2017 |
---|
4 | // Author : Alain Greiner <alain.greiner@lip6.fr> |
---|
5 | /////////////////////////////////////////////////////////////////////////////// |
---|
6 | // This single thread interactive application computes the PGCD. |
---|
7 | /////////////////////////////////////////////////////////////////////////////// |
---|
8 | |
---|
9 | |
---|
10 | #include <stdio.h> |
---|
11 | #include <stdlib.h> |
---|
12 | #include <almosmkh.h> |
---|
13 | |
---|
14 | #define INSTRUMENTATION 0 |
---|
15 | #define IDBG 0 |
---|
16 | |
---|
17 | ///////////////// |
---|
18 | void main( void ) |
---|
19 | { |
---|
20 | int opx; |
---|
21 | int opy; |
---|
22 | int x; |
---|
23 | int y; |
---|
24 | unsigned long long cycle; |
---|
25 | unsigned int cxy; |
---|
26 | unsigned int lid; |
---|
27 | |
---|
28 | get_cycle( &cycle ); |
---|
29 | get_core_id( &cxy , &lid ); |
---|
30 | |
---|
31 | printf( "\n[pgcd] starts on core[%x,%d] / cycle %d\n\n", |
---|
32 | cxy , lid , (unsigned int)cycle ); |
---|
33 | |
---|
34 | // get operand X |
---|
35 | printf("operand X = "); |
---|
36 | opx = get_uint32(); |
---|
37 | printf("\n"); |
---|
38 | |
---|
39 | // get operand Y |
---|
40 | printf("operand Y = "); |
---|
41 | opy = get_uint32(); |
---|
42 | printf("\n"); |
---|
43 | |
---|
44 | // check operands |
---|
45 | if( (opx == 0) || (opy == 0) ) |
---|
46 | { |
---|
47 | printf("\n[pgcd error] operands must be strictly positive\n"); |
---|
48 | exit(0); |
---|
49 | } |
---|
50 | |
---|
51 | // compute PGCD |
---|
52 | x = opx; |
---|
53 | y = opy; |
---|
54 | while (x != y) |
---|
55 | { |
---|
56 | if(x > y) x = x - y; |
---|
57 | else y = y - x; |
---|
58 | } |
---|
59 | |
---|
60 | // display result |
---|
61 | printf("pgcd = %d\n", x); |
---|
62 | |
---|
63 | #if INSTRUMENTATION |
---|
64 | |
---|
65 | char name[64]; |
---|
66 | char path[128]; |
---|
67 | |
---|
68 | // build a file name from X and Y values |
---|
69 | snprintf( name , 64 , "pgcd_%d_%d", opx, opy ); |
---|
70 | |
---|
71 | // build file pathname |
---|
72 | snprintf( path , 128 , "home/%s" , name ); |
---|
73 | |
---|
74 | #if IDBG |
---|
75 | idbg(); |
---|
76 | #endif |
---|
77 | |
---|
78 | // open file |
---|
79 | FILE * stream = fopen( path , NULL ); |
---|
80 | |
---|
81 | if( stream == NULL ) |
---|
82 | { |
---|
83 | printf("\n[pgcd error] cannot open instrumentation file <%s>\n", name ); |
---|
84 | exit(0); |
---|
85 | } |
---|
86 | |
---|
87 | printf("\n[pgcd] file %s successfully open\n", path); |
---|
88 | |
---|
89 | #if IDBG |
---|
90 | idbg(); |
---|
91 | #endif |
---|
92 | |
---|
93 | // register results to file |
---|
94 | int ret = fprintf( stream , "pgcd( %d , %d ) = %d\n", opx, opy, x ); |
---|
95 | |
---|
96 | if( ret < 0 ) |
---|
97 | { |
---|
98 | printf("\n[pgcd error] cannot write to instrumentation file <%s>\n", name ); |
---|
99 | exit(0); |
---|
100 | } |
---|
101 | |
---|
102 | printf("\n[pgcd] file %s successfully written\n", path); |
---|
103 | |
---|
104 | display_mapper( path , 0 , 64 ); |
---|
105 | |
---|
106 | // close instrumentation file |
---|
107 | |
---|
108 | #if IDBG |
---|
109 | idbg(); |
---|
110 | #endif |
---|
111 | |
---|
112 | if( fclose( stream ) ) |
---|
113 | { |
---|
114 | printf("\n[pgcd error] cannot close the file <%s>\n", name ); |
---|
115 | exit(0); |
---|
116 | } |
---|
117 | |
---|
118 | printf("\n[pgcd] file %s successfully closed\n", path); |
---|
119 | |
---|
120 | #endif |
---|
121 | |
---|
122 | exit(0); |
---|
123 | |
---|
124 | } // end pgcd |
---|
125 | |
---|