[417] | 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 | |
---|
[436] | 10 | #include <stdio.h> |
---|
[465] | 11 | #include <stdlib.h> |
---|
[445] | 12 | #include <almosmkh.h> |
---|
[417] | 13 | |
---|
[626] | 14 | #define INSTRUMENTATION 0 |
---|
| 15 | #define IDBG 0 |
---|
| 16 | |
---|
[574] | 17 | ///////////////// |
---|
[475] | 18 | void main( void ) |
---|
[417] | 19 | { |
---|
[657] | 20 | unsigned int opx; |
---|
| 21 | unsigned int opy; |
---|
| 22 | unsigned int x; |
---|
| 23 | unsigned int y; |
---|
[436] | 24 | unsigned long long cycle; |
---|
[580] | 25 | unsigned int cxy; |
---|
| 26 | unsigned int lid; |
---|
[417] | 27 | |
---|
[436] | 28 | get_cycle( &cycle ); |
---|
[637] | 29 | get_core_id( &cxy , &lid ); |
---|
[417] | 30 | |
---|
[626] | 31 | printf( "\n[pgcd] starts on core[%x,%d] / cycle %d\n\n", |
---|
[580] | 32 | cxy , lid , (unsigned int)cycle ); |
---|
| 33 | |
---|
[626] | 34 | // get operand X |
---|
| 35 | printf("operand X = "); |
---|
[657] | 36 | get_uint32( &opx ); |
---|
[626] | 37 | printf("\n"); |
---|
| 38 | |
---|
| 39 | // get operand Y |
---|
| 40 | printf("operand Y = "); |
---|
[657] | 41 | get_uint32( &opy ); |
---|
[626] | 42 | printf("\n"); |
---|
| 43 | |
---|
| 44 | // check operands |
---|
| 45 | if( (opx == 0) || (opy == 0) ) |
---|
[417] | 46 | { |
---|
[626] | 47 | printf("\n[pgcd error] operands must be strictly positive\n"); |
---|
| 48 | exit(0); |
---|
| 49 | } |
---|
[417] | 50 | |
---|
[626] | 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; |
---|
[417] | 58 | } |
---|
[626] | 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 | |
---|
[417] | 124 | } // end pgcd |
---|
| 125 | |
---|