source: trunk/user/pgcd/pgcd.c @ 636

Last change on this file since 636 was 626, checked in by alain, 6 years ago

This version has been tested on the sort multithreaded application
for TSAR_IOB architectures ranging from 1 to 8 clusters.
It fixes three bigs bugs:
1) the dev_ioc device API has been modified: the dev_ioc_sync_read()
and dev_ioc_sync_write() function use now extended pointers on the
kernel buffer to access a mapper stored in any cluster.
2) the hal_uspace API has been modified: the hal_copy_to_uspace()
and hal_copy_from_uspace() functions use now a (cxy,ptr) couple
to identify the target buffer (equivalent to an extended pointer.
3) an implementation bug has been fixed in the assembly code contained
in the hal_copy_to_uspace() and hal_copy_from_uspace() functions.

File size: 2.6 KB
RevLine 
[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]18void main( void )
[417]19{
[436]20    int                opx;
21    int                opy;
[626]22    int                x;
23    int                y;
[436]24    unsigned long long cycle;
[580]25    unsigned int       cxy;
26    unsigned int       lid;
[417]27
[436]28    get_cycle( &cycle );
[580]29    get_core( &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 = ");
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) ) 
[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
75idbg();
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
90idbg();
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
109idbg();
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
Note: See TracBrowser for help on using the repository browser.