linpack_accelerate.c 8.55 KB
Newer Older
Ture Pålsson's avatar
Ture Pålsson committed
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
/*
**
** LINPACK.C        Linpack benchmark, calculates FLOPS.
**                  (FLoating Point Operations Per Second)
**
** Translated to C by Bonnie Toy 5/88
**
** Modified by Will Menninger, 10/93, with these features:
**  (modified on 2/25/94  to fix a problem with daxpy  for
**   unequal increments or equal increments not equal to 1.
**     Jack Dongarra)
**
** - Defaults to double precision.
** - User selectable array sizes.
** - Automatically does enough repetitions to take at least 10 CPU seconds.
** - Prints machine precision.
** - ANSI prototyping.
**
** To compile:  cc -O -o linpack linpack.c -lm
**
**
*/

#include <Accelerate/Accelerate.h>

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
#include <float.h>

32
33
34
35
36
37
38
39
40
41
#if defined(SP)
#define ZERO        0.0
#define ONE         1.0
#define PREC        "Single"
#define BASE10DIG   FLT_DIG

typedef float   REAL;

#elif defined(DP)

Ture Pålsson's avatar
Ture Pålsson committed
42
43
44
45
46
#define ZERO        0.0e0
#define ONE         1.0e0
#define PREC        "Double"
#define BASE10DIG   DBL_DIG

47
48
49
50
typedef double  REAL;

#endif

Ture Pålsson's avatar
Ture Pålsson committed
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
#if defined(__LP64__)
typedef int LPLONG;
#define PLF "d"
#else
typedef long LPLONG;
#define PLF "ld"
#endif

static REAL linpack  (LPLONG nreps,int arsize);
static void matgen   (REAL *a,int lda,int n,REAL *b,REAL *norma);

static void *mempool;

int
main(void)
{
    char    buf[80];
    int     arsize;
    LPLONG    arsize2d, memreq, nreps;
    size_t  malloc_arg;

    while (1)
    {
        printf("Enter array size (q to quit) [200]:  ");
        fgets(buf,79,stdin);
        if (feof(stdin) || buf[0]=='q' || buf[0]=='Q')
            break;
        if (buf[0]=='\0' || buf[0]=='\n')
            arsize=200;
        else
            arsize=atoi(buf);
        arsize/=2;
        arsize*=2;
        if (arsize<10)
        {
            printf("Too small.\n");
            continue;
        }
        arsize2d = (LPLONG)arsize * (LPLONG)arsize;
        memreq = (arsize2d * sizeof(REAL) 
                  + (LPLONG)arsize * sizeof(REAL)
                  + (LPLONG)arsize*sizeof(int));

        printf("Memory required:  %ldK.\n",(memreq+512L)>>10);
        malloc_arg=(size_t)memreq;
        if (malloc_arg!=memreq || (mempool=malloc(malloc_arg))==NULL)
        {
            printf("Not enough memory available for given array size.\n\n");
            continue;
        }
        printf("\n\nLINPACK benchmark, %s precision.\n",PREC);
        printf("Machine precision:  %d digits.\n",BASE10DIG);
103
        printf("Array size %"PLF" X %d.\n",arsize,arsize);
Ture Pålsson's avatar
Ture Pålsson committed
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
        printf("    Reps Time(s) DGEFA   DGESL  OVERHEAD    KFLOPS\n");
        printf("----------------------------------------------------\n");
        nreps=1;
        while (linpack(nreps,arsize)<10.)
            nreps*=2;
        free(mempool);
        printf("\n");
    }
    return 0;
}



/*
**
** DGEFA benchmark
**
** We would like to declare a[][lda], but c does not allow it.  In this
** function, references to a[i][j] are written a[lda*i+j].
**
**   dgefa factors a double precision matrix by gaussian elimination.
**
**   dgefa is usually called by dgeco, but it can be called
**   directly with a saving in time if  rcond  is not needed.
**   (time for dgeco) = (1 + 9/n)*(time for dgefa) .
**
**   on entry
**
**      a       REAL precision[n][lda]
**              the matrix to be factored.
**
**      lda     integer
**              the leading dimension of the array  a .
**
**      n       integer
**              the order of the matrix  a .
**
**   on return
**
**      a       an upper triangular matrix and the multipliers
**              which were used to obtain it.
**              the factorization can be written  a = l*u  where
**              l  is a product of permutation and unit lower
**              triangular matrices and  u  is upper triangular.
**
**      ipvt    integer[n]
**              an integer vector of pivot indices.
**
**      info    integer
**              = 0  normal value.
**              = k  if  u[k][k] .eq. 0.0 .  this is not an error
**                   condition for this subroutine, but it does
**                   indicate that dgesl or dgedi will divide by zero
**                   if called.  use  rcond  in dgeco for a reliable
**                   indication of singularity.
**
**   linpack. this version dated 08/14/78 .
**   cleve moler, university of New Mexico, argonne national lab.
**
**   functions
**
**   blas daxpy,dscal,idamax
**
*/
static void dgefa(REAL *a, LPLONG lda, LPLONG n, LPLONG *ipvt, LPLONG *info)
{
170
#if defined(DP)
Ture Pålsson's avatar
Ture Pålsson committed
171
    dgetrf_(&n, &n, a, &lda, ipvt, info);
172
173
174
#elif defined(SP)
    sgetrf_(&n, &n, a, &lda, ipvt, info);
#endif
Ture Pålsson's avatar
Ture Pålsson committed
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
}

/*
**
** DGESL benchmark
**
** We would like to declare a[][lda], but c does not allow it.  In this
** function, references to a[i][j] are written a[lda*i+j].
**
**   dgesl solves the double precision system
**   a * x = b  or  trans(a) * x = b
**   using the factors computed by dgeco or dgefa.
**
**   on entry
**
**      a       double precision[n][lda]
**              the output from dgeco or dgefa.
**
**      lda     integer
**              the leading dimension of the array  a .
**
**      n       integer
**              the order of the matrix  a .
**
**      ipvt    integer[n]
**              the pivot vector from dgeco or dgefa.
**
**      b       double precision[n]
**              the right hand side vector.
**
**      job     integer
**              = 0         to solve  a*x = b ,
**              = nonzero   to solve  trans(a)*x = b  where
**                          trans(a)  is the transpose.
**
**  on return
**
**      b       the solution vector  x .
**
**   error condition
**
**      a division by zero will occur if the input factor contains a
**      zero on the diagonal.  technically this indicates singularity
**      but it is often caused by improper arguments or improper
**      setting of lda .  it will not occur if the subroutines are
**      called correctly and if dgeco has set rcond .gt. 0.0
**      or dgefa has set info .eq. 0 .
**
**   to compute  inverse(a) * c  where  c  is a matrix
**   with  p  columns
**         dgeco(a,lda,n,ipvt,rcond,z)
**         if (!rcond is too small){
**              for (j=0,j<p,j++)
**                      dgesl(a,lda,n,ipvt,c[j][0],0);
**         }
**
**   linpack. this version dated 08/14/78 .
**   cleve moler, university of new mexico, argonne national lab.
**
**   functions
**
**   blas daxpy,ddot
*/
static void
dgesl(REAL *a, LPLONG lda, LPLONG n, LPLONG *ipvt, REAL *b, int job)
{
    LPLONG info;
    LPLONG nrhs = 1;
    LPLONG ldb = n;
    static char trans = 'N';
245
#if defined(DP)
Ture Pålsson's avatar
Ture Pålsson committed
246
    dgetrs_(&trans, &n, &nrhs, a, &lda, ipvt, b, &ldb, &info);
247
248
249
#elif defined(SP)
    sgetrs_(&trans, &n, &nrhs, a, &lda, ipvt, b, &ldb, &info);
#endif
Ture Pålsson's avatar
Ture Pålsson committed
250
251
252
}


253
254
255
256
257
258
static double
second(void)
{
    return (double)clock() / CLOCKS_PER_SEC;
}

Ture Pålsson's avatar
Ture Pålsson committed
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
static REAL
linpack(LPLONG nreps, int arsize)
{
    REAL  *a, *b;
    REAL   norma, t1, kflops, tdgesl, tdgefa, totalt, toverhead, ops;
    LPLONG   *ipvt,n,info,lda;
    LPLONG   i,arsize2d;

    lda = arsize;
    n = arsize/2;
    arsize2d = (LPLONG)arsize*(LPLONG)arsize;
    ops=((2.0*n*n*n)/3.0+2.0*n*n);
    a=(REAL *)mempool;
    b=a+arsize2d;
    ipvt=(LPLONG *)&b[arsize];
    tdgesl=0;
    tdgefa=0;
    totalt=second();

    for (i = 0; i < nreps; i++)
    {
        matgen(a, lda, n, b, &norma);
        t1 = second();
        dgefa(a, lda, n, ipvt, &info);
        tdgefa += second() - t1;
        t1 = second();
        dgesl(a, lda, n, ipvt, b, 0);
        tdgesl += second() - t1;
    }
    totalt=second()-totalt;
    if (totalt<0.5 || tdgefa+tdgesl<0.2)
        return(0.);

    kflops = nreps * ops / (1000. * (tdgefa + tdgesl));

    toverhead=totalt-tdgefa-tdgesl;

    if (tdgefa<0.)
        tdgefa=0.;
    if (tdgesl<0.)
        tdgesl=0.;
    if (toverhead<0.)
        toverhead=0.;

    printf("%8"PLF" %6.2f %6.2f%% %6.2f%% %6.2f%%  %9.3f\n",
           nreps, totalt, 100.*tdgefa/totalt,
           100.*tdgesl/totalt, 100.*toverhead/totalt,
           kflops);
    return(totalt);
}


/*
** For matgen,
** We would like to declare a[][lda], but c does not allow it.  In this
** function, references to a[i][j] are written a[lda*i+j].
*/
static void matgen(REAL *a,int lda,int n,REAL *b,REAL *norma)

{
    int init,i,j;

    init = 1325;
    *norma = 0.0;
    for (j = 0; j < n; j++)
        for (i = 0; i < n; i++)
        {
            init = (int)((LPLONG)3125*(LPLONG)init % 65536L);
            a[lda*j+i] = (init - 32768.0)/16384.0;
            *norma = (a[lda*j+i] > *norma) ? a[lda*j+i] : *norma;
        }
    for (i = 0; i < n; i++)
        b[i] = 0.0;
    for (j = 0; j < n; j++)
        for (i = 0; i < n; i++)
            b[i] = b[i] + a[lda*j+i];
}