2002-09-26 18:11:06 +00:00
|
|
|
// The contents of this file are subject to the Mozilla Public License
|
|
|
|
// Version 1.0 (the "License"); you may not use this file except in
|
|
|
|
// compliance with the License. You may obtain a copy of the License at
|
|
|
|
// http://www.mozilla.org/MPL/
|
|
|
|
//
|
|
|
|
// Software distributed under the License is distributed on an "AS IS"
|
|
|
|
// basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
|
|
|
|
// License for the specific language governing rights and limitations
|
|
|
|
// under the License.
|
|
|
|
//
|
|
|
|
// The Original Code is the Berkeley Open Infrastructure for Network Computing.
|
|
|
|
//
|
|
|
|
// The Initial Developer of the Original Code is the SETI@home project.
|
|
|
|
// Portions created by the SETI@home project are Copyright (C) 2002
|
|
|
|
// University of California at Berkeley. All Rights Reserved.
|
|
|
|
//
|
|
|
|
// Contributor(s):
|
|
|
|
//
|
|
|
|
|
2002-05-21 23:11:09 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <math.h>
|
|
|
|
#include <time.h>
|
2003-03-20 19:16:28 +00:00
|
|
|
#ifdef HAVE_SYS_TIME_H
|
|
|
|
#include <sys/time.h>
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_SIGNAL_H
|
|
|
|
#include <signal.h>
|
|
|
|
#endif
|
2002-05-29 23:25:21 +00:00
|
|
|
|
2002-05-21 23:11:09 +00:00
|
|
|
#include "speed_stats.h"
|
2002-07-11 01:09:53 +00:00
|
|
|
#include "error_numbers.h"
|
2002-05-21 23:11:09 +00:00
|
|
|
|
2003-03-20 20:00:57 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
#include <afxwin.h>
|
|
|
|
#include <mmsystem.h> // for timing
|
|
|
|
void CALLBACK stop_test(UINT uTimerID, UINT uMsg, DWORD dwUser, DWORD dw1, DWORD dw2);
|
|
|
|
UINT speed_timer_id;
|
|
|
|
#else
|
|
|
|
void stop_test(int a);
|
|
|
|
#endif
|
|
|
|
|
2003-03-20 19:16:28 +00:00
|
|
|
// Speed test global variables
|
2003-05-13 21:20:08 +00:00
|
|
|
// run_test is volatile so the test loops will notice changes made by stop_test
|
|
|
|
volatile bool run_test;
|
2002-05-29 23:25:21 +00:00
|
|
|
|
2002-06-01 20:26:21 +00:00
|
|
|
//#define RUN_TEST
|
2002-05-29 23:25:21 +00:00
|
|
|
|
2002-05-21 23:11:09 +00:00
|
|
|
#ifdef RUN_TEST
|
|
|
|
|
2002-09-26 18:11:06 +00:00
|
|
|
int main(void) {
|
2002-05-29 23:25:21 +00:00
|
|
|
int cache_size;
|
|
|
|
|
2002-09-26 18:11:06 +00:00
|
|
|
cache_size = check_cache_size(CACHE_MAX);
|
2002-05-21 23:11:09 +00:00
|
|
|
|
2002-09-26 18:11:06 +00:00
|
|
|
run_test_suite(4);
|
2002-05-29 23:25:21 +00:00
|
|
|
|
|
|
|
return 0;
|
2002-05-21 23:11:09 +00:00
|
|
|
}
|
|
|
|
|
2002-09-26 18:11:06 +00:00
|
|
|
void run_test_suite(double num_secs_per_test) {
|
|
|
|
if (num_secs_per_test<0) {
|
2002-07-15 23:21:20 +00:00
|
|
|
fprintf(stderr, "error: run_test_suite: negative num_seconds_per_test\n");
|
|
|
|
}
|
|
|
|
printf(
|
|
|
|
"Running tests. This will take about %.1lf seconds.\n\n",
|
|
|
|
num_secs_per_test*3
|
2002-10-20 22:12:53 +00:00
|
|
|
);
|
2002-07-15 23:21:20 +00:00
|
|
|
|
|
|
|
printf(
|
|
|
|
"Speed: %.5lf million flops/sec\n\n",
|
|
|
|
run_double_prec_test(num_secs_per_test)/1000000
|
2002-10-20 22:12:53 +00:00
|
|
|
);
|
2002-07-15 23:21:20 +00:00
|
|
|
printf(
|
|
|
|
"Speed: %.5lf million integer ops/sec\n\n",
|
|
|
|
run_int_test(num_secs_per_test)/1000000
|
2002-10-20 22:12:53 +00:00
|
|
|
);
|
2002-07-15 23:21:20 +00:00
|
|
|
printf(
|
|
|
|
"Speed: %.5lf MB/sec\n\n",
|
2003-03-20 19:16:28 +00:00
|
|
|
run_mem_bandwidth_test(num_secs_per_test)/1000000
|
2002-10-20 22:12:53 +00:00
|
|
|
);
|
2002-07-15 23:21:20 +00:00
|
|
|
}
|
|
|
|
|
2002-05-21 23:11:09 +00:00
|
|
|
#endif
|
|
|
|
|
2002-09-26 18:11:06 +00:00
|
|
|
int check_cache_size(int mem_size) {
|
2002-05-21 23:11:09 +00:00
|
|
|
int i, n, index, stride, *memBlock, logStride, logCache;
|
|
|
|
double **results;
|
|
|
|
int steps, tsteps, csize, limit, temp, cind, sind;
|
|
|
|
clock_t total_sec, sec;
|
2002-05-29 23:25:21 +00:00
|
|
|
double secs, nanosecs, temp2;
|
2002-05-21 23:11:09 +00:00
|
|
|
int not_found;
|
2002-09-26 18:11:06 +00:00
|
|
|
if (mem_size<0) {
|
2002-07-11 01:09:53 +00:00
|
|
|
fprintf(stderr, "error: check_cache_size: negative mem_size\n");
|
|
|
|
return ERR_NEG;
|
|
|
|
}
|
2002-11-04 04:13:21 +00:00
|
|
|
logStride = (int)(log((double)(STRIDE_MAX/STRIDE_MIN))/log(2.0))+1;
|
|
|
|
logCache = (int)(log((double)(CACHE_MAX/CACHE_MIN))/log(2.0))+1;
|
2002-05-21 23:11:09 +00:00
|
|
|
|
2002-09-26 18:11:06 +00:00
|
|
|
printf("Test will take about %.2f seconds.\n", SECS_PER_RUN*logStride*logCache);
|
|
|
|
results = (double **)malloc(sizeof(double *)*logStride);
|
2002-05-21 23:11:09 +00:00
|
|
|
|
2002-09-26 18:11:06 +00:00
|
|
|
for (i=0;i<logStride;i++) {
|
|
|
|
results[i] = (double *)malloc(sizeof(double)*logCache);
|
|
|
|
for (n=0;n<logCache;n++) {
|
2002-05-21 23:11:09 +00:00
|
|
|
results[i][n] = 1.0;
|
2002-05-29 23:25:21 +00:00
|
|
|
}
|
2002-05-21 23:11:09 +00:00
|
|
|
}
|
|
|
|
|
2002-09-26 18:11:06 +00:00
|
|
|
printf("|");
|
|
|
|
for (i=0;i<logCache;i++) {
|
|
|
|
printf("-");
|
2002-05-29 23:25:21 +00:00
|
|
|
}
|
2002-05-21 23:11:09 +00:00
|
|
|
|
2002-09-26 18:11:06 +00:00
|
|
|
printf("|\n");
|
|
|
|
memBlock = (int *)malloc(sizeof(int)*mem_size);
|
|
|
|
printf(" ");
|
2002-05-21 23:11:09 +00:00
|
|
|
|
2002-09-26 18:11:06 +00:00
|
|
|
for (csize=CACHE_MIN,cind=0;csize<=CACHE_MAX;csize*=2,cind++) {
|
|
|
|
for (stride = STRIDE_MIN,sind=0; stride<=STRIDE_MAX; stride*=2,sind++) {
|
2002-05-29 23:25:21 +00:00
|
|
|
limit = csize - stride + 1; // cache size this loop
|
2002-05-21 23:11:09 +00:00
|
|
|
|
|
|
|
steps = 0;
|
|
|
|
sec = clock();
|
2002-05-29 23:25:21 +00:00
|
|
|
do { // repeat until collect 1 second
|
2002-09-26 18:11:06 +00:00
|
|
|
for (i = SAMPLE * stride; i != 0; i--) { // larger sample
|
2002-05-29 23:25:21 +00:00
|
|
|
for (index = 0; index < limit; index += stride) {
|
|
|
|
memBlock[index]++; // cache access
|
|
|
|
}
|
|
|
|
}
|
|
|
|
steps++; // count while loop iterations
|
|
|
|
} while (clock() < sec+(CLOCKS_PER_SEC*SECS_PER_RUN)); // until collect 1 second
|
2002-05-21 23:11:09 +00:00
|
|
|
total_sec = clock()-sec;
|
|
|
|
|
2002-05-29 23:25:21 +00:00
|
|
|
// Repeat empty loop to loop subtract overhead
|
|
|
|
tsteps = 0; // used to match no. while iterations
|
2002-05-21 23:11:09 +00:00
|
|
|
temp = 0;
|
|
|
|
sec = clock();
|
2002-05-29 23:25:21 +00:00
|
|
|
do { // repeat until same no. iterations as above
|
2002-09-26 18:11:06 +00:00
|
|
|
for (i = SAMPLE * stride; i != 0; i--) { // larger sample
|
2002-05-21 23:11:09 +00:00
|
|
|
for (index = 0; index < limit; index += stride) {
|
2002-05-29 23:25:21 +00:00
|
|
|
temp += index; // dummy code
|
2002-05-21 23:11:09 +00:00
|
|
|
}
|
|
|
|
}
|
2002-05-29 23:25:21 +00:00
|
|
|
tsteps++; // count while iterations
|
|
|
|
} while (tsteps < steps); // until = no. iterations
|
2002-05-21 23:11:09 +00:00
|
|
|
total_sec -= clock()-sec;
|
|
|
|
|
|
|
|
secs = ((double)total_sec) / CLOCKS_PER_SEC;
|
|
|
|
|
2002-09-26 18:11:06 +00:00
|
|
|
if (temp == 3) {
|
|
|
|
printf("Howdy\n");
|
2002-05-29 23:25:21 +00:00
|
|
|
}
|
2002-05-21 23:11:09 +00:00
|
|
|
|
|
|
|
nanosecs = (double) secs * 1e9 / (steps * SAMPLE * stride * ((limit - 1) / stride + 1));
|
|
|
|
results[sind][cind] = nanosecs;
|
2002-05-29 23:25:21 +00:00
|
|
|
|
2002-09-26 18:11:06 +00:00
|
|
|
//if (stride==STRIDE_MIN) printf("\n");
|
2002-05-29 23:25:21 +00:00
|
|
|
printf(
|
|
|
|
"Size (bytes): %7d Stride (bytes): %4d read+write: %4.0f ns, %d %d\n",
|
2003-06-10 19:45:58 +00:00
|
|
|
(int)(csize * sizeof (int)), (int)(stride * sizeof(int)), nanosecs, sind, cind
|
2002-09-26 18:11:06 +00:00
|
|
|
);
|
2002-05-21 23:11:09 +00:00
|
|
|
}
|
2002-09-26 18:11:06 +00:00
|
|
|
printf(".");
|
|
|
|
fflush(stdout);
|
2002-05-21 23:11:09 +00:00
|
|
|
}
|
2002-09-26 18:11:06 +00:00
|
|
|
printf("\n");
|
2002-05-21 23:11:09 +00:00
|
|
|
|
2002-09-26 18:11:06 +00:00
|
|
|
for (i=0;i<logStride;i++) {
|
|
|
|
for (n=0;n<logCache;n++) {
|
2002-05-21 23:11:09 +00:00
|
|
|
printf ("%4.0f ", results[i][n]);
|
2002-05-29 23:25:21 +00:00
|
|
|
}
|
2002-09-26 18:11:06 +00:00
|
|
|
printf("\n");
|
2002-05-29 23:25:21 +00:00
|
|
|
}
|
2002-05-21 23:11:09 +00:00
|
|
|
|
2002-09-26 18:11:06 +00:00
|
|
|
for (i=0;i<logStride;i++) {
|
|
|
|
for (n=logCache;n>0;n--) {
|
2002-05-21 23:11:09 +00:00
|
|
|
results[i][n] /= results[i][n-1];
|
2002-05-29 23:25:21 +00:00
|
|
|
}
|
|
|
|
}
|
2002-05-21 23:11:09 +00:00
|
|
|
|
2002-09-26 18:11:06 +00:00
|
|
|
for (i=0;i<logCache;i++) {
|
2002-05-21 23:11:09 +00:00
|
|
|
temp2 = 0;
|
2002-09-26 18:11:06 +00:00
|
|
|
for (n=0;n<logStride;n++) {
|
2002-05-21 23:11:09 +00:00
|
|
|
temp2 += results[n][i];
|
|
|
|
}
|
|
|
|
results[0][i] = temp2/logStride;
|
|
|
|
}
|
|
|
|
|
2002-09-26 18:11:06 +00:00
|
|
|
printf("\n");
|
|
|
|
for (i=0;i<logStride;i++) {
|
|
|
|
for (n=1;n<logCache;n++) {
|
2002-05-21 23:11:09 +00:00
|
|
|
printf ("%1.3f ", results[i][n]);
|
2002-05-29 23:25:21 +00:00
|
|
|
}
|
2002-09-26 18:11:06 +00:00
|
|
|
printf("\n");
|
2002-05-29 23:25:21 +00:00
|
|
|
}
|
2002-05-21 23:11:09 +00:00
|
|
|
|
|
|
|
csize=CACHE_MIN;
|
|
|
|
i = 1;
|
|
|
|
not_found = 2;
|
2002-09-26 18:11:06 +00:00
|
|
|
while(not_found && i < logCache) {
|
|
|
|
if (not_found == 1 && results[0][i] > 1.5) {
|
2003-06-10 19:45:58 +00:00
|
|
|
printf("Level 2 Data Cache is %d KB.\n", (int)(csize*sizeof(int)/CACHE_MIN));
|
2002-05-21 23:11:09 +00:00
|
|
|
not_found = 0;
|
|
|
|
}
|
2002-09-26 18:11:06 +00:00
|
|
|
if (not_found == 2 && results[0][i] > 1.5) {
|
2003-06-10 19:45:58 +00:00
|
|
|
printf("Level 1 Data Cache is %d KB.\n", (int)(csize*sizeof(int)/CACHE_MIN));
|
2002-05-21 23:11:09 +00:00
|
|
|
not_found = 1;
|
|
|
|
}
|
|
|
|
i++;
|
|
|
|
csize *= 2;
|
|
|
|
}
|
|
|
|
|
2002-09-26 18:11:06 +00:00
|
|
|
free(memBlock);
|
|
|
|
for (i=0;i<logStride;i++)
|
|
|
|
free(results[i]);
|
2002-05-21 23:11:09 +00:00
|
|
|
|
2002-09-26 18:11:06 +00:00
|
|
|
free(results);
|
2002-05-21 23:11:09 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2003-03-20 19:16:28 +00:00
|
|
|
// Run the test of double precision math speed for num_secs seconds
|
2002-07-15 23:21:20 +00:00
|
|
|
//
|
2003-06-19 18:19:35 +00:00
|
|
|
int run_double_prec_test(double num_secs, double &flops_per_sec) {
|
|
|
|
int retval;
|
2003-03-20 19:16:28 +00:00
|
|
|
|
2002-09-26 18:11:06 +00:00
|
|
|
if (num_secs<0) {
|
2003-03-20 19:16:28 +00:00
|
|
|
fprintf(stderr, "error: run_double_prec_test: negative num_secs\n");
|
2002-07-11 01:09:53 +00:00
|
|
|
return ERR_NEG;
|
|
|
|
}
|
2003-03-20 19:16:28 +00:00
|
|
|
|
|
|
|
// Setup a timer to interrupt the tests in num_secs
|
|
|
|
set_test_timer(num_secs);
|
2002-05-21 23:11:09 +00:00
|
|
|
|
2003-06-19 18:19:35 +00:00
|
|
|
retval = (int)double_flop_test(0, flops_per_sec, 0);
|
2002-05-21 23:11:09 +00:00
|
|
|
|
2003-03-20 19:16:28 +00:00
|
|
|
destroy_test_timer();
|
2002-05-21 23:11:09 +00:00
|
|
|
|
2003-06-19 18:19:35 +00:00
|
|
|
return retval;
|
2002-05-21 23:11:09 +00:00
|
|
|
}
|
|
|
|
|
2003-03-20 19:16:28 +00:00
|
|
|
// Run the test of integer math speed for num_secs seconds
|
2002-07-15 23:21:20 +00:00
|
|
|
//
|
2003-06-19 18:19:35 +00:00
|
|
|
int run_int_test(double num_secs, double &iops_per_sec) {
|
|
|
|
int retval;
|
2003-03-20 19:16:28 +00:00
|
|
|
|
2002-09-26 18:11:06 +00:00
|
|
|
if (num_secs<0) {
|
2002-07-11 01:09:53 +00:00
|
|
|
fprintf(stderr, "error: run_int_test: negative num_secs\n");
|
|
|
|
return ERR_NEG;
|
|
|
|
}
|
2003-03-20 19:16:28 +00:00
|
|
|
|
|
|
|
// Setup a timer to interrupt the tests in num_secs
|
|
|
|
set_test_timer(num_secs);
|
2002-05-21 23:11:09 +00:00
|
|
|
|
2003-06-19 18:19:35 +00:00
|
|
|
retval = (int)int_op_test(0, iops_per_sec, 0);
|
2002-05-21 23:11:09 +00:00
|
|
|
|
2003-03-20 19:16:28 +00:00
|
|
|
destroy_test_timer();
|
2002-05-21 23:11:09 +00:00
|
|
|
|
2003-06-19 18:19:35 +00:00
|
|
|
return retval;
|
2002-05-21 23:11:09 +00:00
|
|
|
}
|
|
|
|
|
2003-03-20 19:16:28 +00:00
|
|
|
// Run the test of memory bandwidth speed for num_secs seconds
|
2002-07-15 23:21:20 +00:00
|
|
|
//
|
2003-06-19 18:19:35 +00:00
|
|
|
int run_mem_bandwidth_test(double num_secs, double &bytes_per_sec) {
|
|
|
|
int retval;
|
2003-03-20 19:16:28 +00:00
|
|
|
|
2002-09-26 18:11:06 +00:00
|
|
|
if (num_secs<0) {
|
2002-07-11 01:09:53 +00:00
|
|
|
fprintf(stderr, "error: run_mem_bandwidth_test: negative num_secs\n");
|
|
|
|
return ERR_NEG;
|
|
|
|
}
|
2002-05-21 23:11:09 +00:00
|
|
|
|
2003-03-20 19:16:28 +00:00
|
|
|
// Setup a timer to interrupt the tests in num_secs
|
|
|
|
set_test_timer(num_secs);
|
2002-05-21 23:11:09 +00:00
|
|
|
|
2003-06-19 18:19:35 +00:00
|
|
|
retval = (int)bandwidth_test(0, bytes_per_sec, 0);
|
2003-03-20 19:16:28 +00:00
|
|
|
|
|
|
|
destroy_test_timer();
|
|
|
|
|
2003-06-19 18:19:35 +00:00
|
|
|
return retval;
|
2002-05-21 23:11:09 +00:00
|
|
|
}
|
|
|
|
|
2002-05-29 23:25:21 +00:00
|
|
|
// One iteration == D_LOOP_ITERS (1,000,000) floating point operations
|
2002-07-15 17:28:40 +00:00
|
|
|
// If time_total is negative, there was an error in the calculation,
|
|
|
|
// meaning there is probably something wrong with the CPU
|
2002-05-21 23:11:09 +00:00
|
|
|
|
2003-06-19 18:19:35 +00:00
|
|
|
int double_flop_test(int iterations, double &flops_per_sec, int print_debug) {
|
|
|
|
double a[NUM_DOUBLES], b[NUM_DOUBLES], dp, temp;
|
|
|
|
int i,n,j,actual_iters, error = 0;
|
|
|
|
clock_t time_start, time_total;
|
2003-03-20 19:16:28 +00:00
|
|
|
|
2002-09-26 18:11:06 +00:00
|
|
|
if (iterations<0) {
|
2002-07-11 01:09:53 +00:00
|
|
|
fprintf(stderr, "error: double_flop_test: negative iterations\n");
|
|
|
|
return ERR_NEG;
|
|
|
|
}
|
2002-05-21 23:11:09 +00:00
|
|
|
|
2003-03-20 19:16:28 +00:00
|
|
|
// If iterations is 0, assume we're using the timer
|
|
|
|
if (iterations == 0) {
|
2003-03-24 19:55:52 +00:00
|
|
|
run_test = true;
|
2003-03-20 19:16:28 +00:00
|
|
|
iterations = 200000000;
|
|
|
|
}
|
|
|
|
|
|
|
|
a[0] = b[0] = 1.0;
|
|
|
|
|
|
|
|
for (i=1;i<NUM_DOUBLES;i++) {
|
2003-06-19 18:19:35 +00:00
|
|
|
a[i] = a[i-1] / 2.0;
|
|
|
|
b[i] = b[i-1] * 2.0;
|
2003-03-20 19:16:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
actual_iters = 0;
|
|
|
|
|
2002-05-21 23:11:09 +00:00
|
|
|
time_start = clock();
|
2003-03-20 19:16:28 +00:00
|
|
|
|
|
|
|
for (n=0;(n<iterations)&&run_test;n++) {
|
|
|
|
for (j=0;j<D_LOOP_ITERS;j+=((NUM_DOUBLES*4)+1)) {
|
|
|
|
dp = 0;
|
2003-06-19 18:19:35 +00:00
|
|
|
for (i=0;i<NUM_DOUBLES;i++) { // 2*NUM_DOUBLES flops
|
|
|
|
dp += a[i]*b[i]; // 2 flops
|
2003-03-20 19:16:28 +00:00
|
|
|
}
|
2003-06-19 18:19:35 +00:00
|
|
|
dp /= (float)NUM_DOUBLES; // 1 flop
|
|
|
|
for (i=0;i<NUM_DOUBLES;i++) { // 2*NUM_DOUBLES flops
|
|
|
|
a[i] *= dp; // 1 flop
|
|
|
|
b[i] *= dp; // 1 flop
|
2002-05-29 23:25:21 +00:00
|
|
|
}
|
2002-05-21 23:11:09 +00:00
|
|
|
}
|
2003-03-20 19:16:28 +00:00
|
|
|
actual_iters++;
|
2002-05-21 23:11:09 +00:00
|
|
|
}
|
|
|
|
|
2002-07-15 23:21:20 +00:00
|
|
|
time_total = clock();
|
|
|
|
|
|
|
|
// Accomodate for the possibility of clock wraparound
|
2002-09-26 18:11:06 +00:00
|
|
|
if (time_total > time_start) {
|
2002-07-15 23:21:20 +00:00
|
|
|
time_total -= time_start;
|
|
|
|
} else {
|
|
|
|
time_total = 0; // this is just a kludge
|
|
|
|
}
|
|
|
|
|
2003-06-19 18:19:35 +00:00
|
|
|
flops_per_sec = D_LOOP_ITERS*actual_iters/((double)time_total/CLOCKS_PER_SEC);
|
2002-05-21 23:11:09 +00:00
|
|
|
|
|
|
|
temp = 1;
|
2002-07-15 23:21:20 +00:00
|
|
|
// Check to make sure all the values are the same as when we started
|
2002-09-26 18:11:06 +00:00
|
|
|
for (i=0;i<NUM_DOUBLES;i++) {
|
2003-06-19 18:19:35 +00:00
|
|
|
if ((double)a[i] != (float)temp) error = ERR_BENCHMARK_FAILED;
|
2002-07-15 17:28:40 +00:00
|
|
|
temp /= 2;
|
2002-05-21 23:11:09 +00:00
|
|
|
}
|
|
|
|
|
2002-09-26 18:11:06 +00:00
|
|
|
if (print_debug) {
|
|
|
|
for (i=0;i<NUM_DOUBLES;i++) {
|
2002-10-20 22:12:53 +00:00
|
|
|
printf("%3d: %.50f\n", i, a[i]);
|
2002-05-29 23:25:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-06-19 18:19:35 +00:00
|
|
|
return error;
|
2002-05-21 23:11:09 +00:00
|
|
|
}
|
|
|
|
|
2002-05-29 23:25:21 +00:00
|
|
|
// One iteration == 1,000,000 integer operations
|
2002-07-15 17:28:40 +00:00
|
|
|
// If time_total is negative, there was an error in the calculation,
|
|
|
|
// meaning there is probably something wrong with the CPU
|
2002-05-21 23:11:09 +00:00
|
|
|
|
2003-06-19 18:19:35 +00:00
|
|
|
int int_op_test(int iterations, double &iops_per_sec, int print_debug) {
|
2003-03-20 19:16:28 +00:00
|
|
|
int a[NUM_INTS], temp, actual_iters;
|
2002-05-21 23:11:09 +00:00
|
|
|
clock_t time_start, time_total;
|
2003-06-19 18:19:35 +00:00
|
|
|
int i,j,k,error = 0;
|
2002-09-26 18:11:06 +00:00
|
|
|
if (iterations<0) {
|
2002-07-11 01:09:53 +00:00
|
|
|
fprintf(stderr, "error: int_op_test: negative iterations\n");
|
|
|
|
return ERR_NEG;
|
|
|
|
}
|
2003-03-20 19:16:28 +00:00
|
|
|
|
|
|
|
// If iterations is 0, assume we're using the timer
|
|
|
|
if (iterations == 0) {
|
2003-03-24 19:55:52 +00:00
|
|
|
run_test = true;
|
2003-03-20 19:16:28 +00:00
|
|
|
iterations = 200000000;
|
|
|
|
}
|
|
|
|
|
2002-05-29 23:25:21 +00:00
|
|
|
a[0] = 1;
|
2002-09-26 18:11:06 +00:00
|
|
|
for (i=1;i<NUM_INTS;i++) {
|
2002-05-21 23:11:09 +00:00
|
|
|
a[i] = 2*a[i-1];
|
|
|
|
}
|
2002-06-12 19:24:45 +00:00
|
|
|
|
2003-03-20 19:16:28 +00:00
|
|
|
actual_iters = 0;
|
|
|
|
|
2002-05-21 23:11:09 +00:00
|
|
|
time_start = clock();
|
2003-03-20 19:16:28 +00:00
|
|
|
for (i=0;(i<iterations) && run_test;i++) {
|
2002-07-15 23:21:20 +00:00
|
|
|
// The contents of the array "a" should be the same at the
|
|
|
|
// beginning and end of each loop iteration. Most compilers will
|
|
|
|
// partially unroll the individual loops within this one, so
|
|
|
|
// those integer operations (incrementing k) are not counted
|
2002-09-26 18:11:06 +00:00
|
|
|
for (j=0;j<I_LOOP_ITERS/(NUM_INTS*9);j++) {
|
|
|
|
for (k=0;k<NUM_INTS;k++) {
|
2002-06-12 19:24:45 +00:00
|
|
|
a[k] *= 3; // 1 int ops
|
2002-05-29 23:25:21 +00:00
|
|
|
}
|
2002-09-26 18:11:06 +00:00
|
|
|
for (k=NUM_INTS-1;k>=0;k--) {
|
2002-05-29 23:25:21 +00:00
|
|
|
a[k] += 6; // 2 int ops
|
|
|
|
}
|
2002-09-26 18:11:06 +00:00
|
|
|
for (k=0;k<NUM_INTS;k++) {
|
2002-06-12 19:24:45 +00:00
|
|
|
a[k] /= 3; // 3 int ops
|
2002-05-29 23:25:21 +00:00
|
|
|
}
|
2002-09-26 18:11:06 +00:00
|
|
|
for (k=NUM_INTS-1;k>=0;k--) {
|
2002-06-12 19:24:45 +00:00
|
|
|
a[k] -= 2; // 4 int ops
|
|
|
|
}
|
2002-09-26 18:11:06 +00:00
|
|
|
for (k=NUM_INTS-1;k>0;k--) {
|
2002-06-12 19:24:45 +00:00
|
|
|
a[k] -= a[k-1]; // 5 int ops
|
2002-05-29 23:25:21 +00:00
|
|
|
}
|
2002-09-26 18:11:06 +00:00
|
|
|
for (k=1;k<NUM_INTS;k++) {
|
2002-06-12 19:24:45 +00:00
|
|
|
a[k] = 2*a[k-1]; // 6 int ops
|
2002-05-29 23:25:21 +00:00
|
|
|
}
|
2002-09-26 18:11:06 +00:00
|
|
|
for (k=NUM_INTS-1;k>0;k--) {
|
|
|
|
if (a[k-1] != 0) // 7 int ops
|
2002-06-12 19:24:45 +00:00
|
|
|
a[k] /= a[k-1]; // 8 int ops
|
2002-05-21 23:11:09 +00:00
|
|
|
}
|
2002-09-26 18:11:06 +00:00
|
|
|
for (k=1;k<NUM_INTS;k++) {
|
2002-06-12 19:24:45 +00:00
|
|
|
a[k] = 2*a[k-1]; // 9 int ops
|
2002-05-29 23:25:21 +00:00
|
|
|
}
|
2002-05-21 23:11:09 +00:00
|
|
|
}
|
2003-03-20 19:16:28 +00:00
|
|
|
actual_iters++;
|
2002-05-21 23:11:09 +00:00
|
|
|
}
|
|
|
|
|
2002-07-15 23:21:20 +00:00
|
|
|
// Stop the clock
|
|
|
|
time_total = clock();
|
|
|
|
|
|
|
|
// Accomodate for the possibility of clock wraparound
|
2002-09-26 18:11:06 +00:00
|
|
|
if (time_total > time_start) {
|
2002-07-15 23:21:20 +00:00
|
|
|
time_total -= time_start;
|
|
|
|
} else {
|
|
|
|
time_total = 0; // this is just a kludge
|
|
|
|
}
|
2002-05-21 23:11:09 +00:00
|
|
|
|
2003-06-19 18:19:35 +00:00
|
|
|
iops_per_sec = I_LOOP_ITERS*actual_iters/((double)time_total/CLOCKS_PER_SEC);
|
2003-03-20 19:16:28 +00:00
|
|
|
|
2002-05-21 23:11:09 +00:00
|
|
|
temp = 1;
|
2002-07-15 23:21:20 +00:00
|
|
|
// Check to make sure all the values are the same as when we started
|
2002-09-26 18:11:06 +00:00
|
|
|
for (i=0;i<NUM_INTS;i++) {
|
2003-06-19 18:19:35 +00:00
|
|
|
if (a[i] != temp) error = ERR_BENCHMARK_FAILED;
|
2002-05-21 23:11:09 +00:00
|
|
|
temp *= 2;
|
|
|
|
}
|
|
|
|
|
2002-09-26 18:11:06 +00:00
|
|
|
if (print_debug) {
|
|
|
|
for (i=0;i<NUM_INTS;i++) {
|
|
|
|
printf("%3d: %d\n", i, a[i]);
|
2002-05-29 23:25:21 +00:00
|
|
|
}
|
|
|
|
}
|
2002-05-21 23:11:09 +00:00
|
|
|
|
2003-06-19 18:19:35 +00:00
|
|
|
return error;
|
2002-05-21 23:11:09 +00:00
|
|
|
}
|
|
|
|
|
2003-03-20 19:16:28 +00:00
|
|
|
// If return value is negative, there was an error in the copying,
|
2002-07-15 17:28:40 +00:00
|
|
|
// meaning there is probably something wrong with the CPU
|
2003-03-20 19:16:28 +00:00
|
|
|
//
|
2003-06-19 18:19:35 +00:00
|
|
|
int bandwidth_test(int iterations, double &bytes_per_sec, int print_debug) {
|
2002-05-29 23:25:21 +00:00
|
|
|
// a, b, and c are arrays of doubles we will copy around to test memory bandwidth
|
2002-05-21 23:11:09 +00:00
|
|
|
double *a, *b, *c;
|
2002-05-29 23:25:21 +00:00
|
|
|
// aVal and bVal are the values of all elements of a and b.
|
2002-05-21 23:11:09 +00:00
|
|
|
double aVal, bVal;
|
2002-05-29 23:25:21 +00:00
|
|
|
// Start and stop times for the clock
|
2002-05-21 23:11:09 +00:00
|
|
|
clock_t time_start, time_total;
|
2003-06-19 18:19:35 +00:00
|
|
|
int i,j,n,actual_iters, error = 0;
|
2002-09-26 18:11:06 +00:00
|
|
|
if (iterations<0) {
|
2002-07-11 01:09:53 +00:00
|
|
|
fprintf(stderr, "error: bandwidth_test: negative iterations\n");
|
|
|
|
return ERR_NEG;
|
|
|
|
}
|
2003-03-20 19:16:28 +00:00
|
|
|
|
|
|
|
// If iterations is 0, assume we're using the timer
|
|
|
|
if (iterations == 0) {
|
2003-03-24 19:55:52 +00:00
|
|
|
run_test = true;
|
2003-03-20 19:16:28 +00:00
|
|
|
iterations = 200000000;
|
|
|
|
}
|
|
|
|
|
2002-05-29 23:25:21 +00:00
|
|
|
// These are doubles in order to make full use of bus and instruction bandwidth
|
2002-09-26 18:11:06 +00:00
|
|
|
a = (double *)malloc(MEM_SIZE * sizeof(double));
|
|
|
|
b = (double *)malloc(MEM_SIZE * sizeof(double));
|
|
|
|
c = (double *)malloc(MEM_SIZE * sizeof(double));
|
2002-05-29 23:25:21 +00:00
|
|
|
|
|
|
|
// These values use all the bits in a floating point number (Investigate these values)
|
|
|
|
aVal = (-2.0/3.0)*pow(2.0,-341.0);
|
|
|
|
bVal = (1.0/3.0)*pow(2.0,342.0);
|
|
|
|
|
|
|
|
// We add i to each value to prevent compiler optimizations of the copy
|
2002-09-26 18:11:06 +00:00
|
|
|
for (i=0;i<MEM_SIZE;i++) {
|
2002-05-21 23:11:09 +00:00
|
|
|
a[i] = aVal+i; b[i] = bVal+i; c[i] = 1.0;
|
|
|
|
}
|
|
|
|
|
2003-03-20 19:16:28 +00:00
|
|
|
actual_iters = 0;
|
|
|
|
|
2002-05-29 23:25:21 +00:00
|
|
|
// Start the clock
|
2002-05-21 23:11:09 +00:00
|
|
|
time_start = clock();
|
|
|
|
|
2003-03-20 19:16:28 +00:00
|
|
|
// One iteration == Read of 6,000,000*sizeof(double), Write of 6,000,000*sizeof(double)
|
2002-05-29 23:25:21 +00:00
|
|
|
// 6 read, 6 write operations per iteration which will preserve a and b
|
2003-03-20 19:16:28 +00:00
|
|
|
for (i=0;(i<iterations) && run_test;i++) {
|
|
|
|
for (n=0;n<2;n++) {
|
|
|
|
for (j=0;j<MEM_SIZE;j++) {
|
|
|
|
c[j] = a[j];
|
|
|
|
a[j] = b[j];
|
|
|
|
b[j] = c[j];
|
|
|
|
}
|
2002-05-21 23:11:09 +00:00
|
|
|
}
|
2003-03-20 19:16:28 +00:00
|
|
|
actual_iters++;
|
2002-05-21 23:11:09 +00:00
|
|
|
}
|
|
|
|
|
2002-05-29 23:25:21 +00:00
|
|
|
// Stop the clock
|
2002-05-21 23:11:09 +00:00
|
|
|
time_total = clock();
|
|
|
|
|
2002-05-29 23:25:21 +00:00
|
|
|
// Accomodate for the possibility of clock wraparound
|
2002-09-26 18:11:06 +00:00
|
|
|
if (time_total > time_start) {
|
2002-05-21 23:11:09 +00:00
|
|
|
time_total -= time_start;
|
2002-05-29 23:25:21 +00:00
|
|
|
} else {
|
2002-07-15 23:21:20 +00:00
|
|
|
time_total = 0; // this is just a kludge
|
2002-05-29 23:25:21 +00:00
|
|
|
}
|
2002-05-21 23:11:09 +00:00
|
|
|
|
2003-06-19 18:19:35 +00:00
|
|
|
bytes_per_sec = 2.0*6.0*MEM_SIZE*actual_iters*sizeof(double)/((double)time_total/CLOCKS_PER_SEC);
|
2003-03-20 19:16:28 +00:00
|
|
|
|
2002-09-26 18:11:06 +00:00
|
|
|
for (i=0;i<MEM_SIZE;i++) {
|
2003-06-19 18:19:35 +00:00
|
|
|
if (a[i] != aVal+i || b[i] != bVal+i) error = ERR_BENCHMARK_FAILED;
|
2002-05-21 23:11:09 +00:00
|
|
|
}
|
|
|
|
|
2002-09-26 18:11:06 +00:00
|
|
|
free(a);
|
|
|
|
free(b);
|
|
|
|
free(c);
|
2002-05-21 23:11:09 +00:00
|
|
|
|
2003-06-19 18:19:35 +00:00
|
|
|
return error;
|
2003-03-20 19:16:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: handle errors here
|
|
|
|
int set_test_timer(double num_secs) {
|
|
|
|
run_test = true;
|
|
|
|
#ifdef _WIN32
|
2003-03-20 20:00:57 +00:00
|
|
|
speed_timer_id = timeSetEvent( (int)(num_secs*1000),
|
|
|
|
(int)(num_secs*1000), stop_test, NULL, TIME_ONESHOT );
|
2003-03-20 19:16:28 +00:00
|
|
|
#else
|
|
|
|
itimerval value;
|
2003-05-13 21:20:08 +00:00
|
|
|
signal(SIGALRM, stop_test);
|
2003-03-20 19:16:28 +00:00
|
|
|
value.it_value.tv_sec = (int)num_secs;
|
|
|
|
value.it_value.tv_usec = ((int)(num_secs*1000000))%1000000;
|
|
|
|
value.it_interval = value.it_value;
|
2003-05-13 21:20:08 +00:00
|
|
|
setitimer(ITIMER_REAL, &value, NULL);
|
2003-03-20 19:16:28 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int destroy_test_timer() {
|
|
|
|
#ifdef _WIN32
|
2003-06-19 18:19:35 +00:00
|
|
|
timeKillEvent(speed_timer_id);
|
2003-03-20 19:16:28 +00:00
|
|
|
#endif
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
void CALLBACK stop_test(UINT uTimerID, UINT uMsg, DWORD dwUser, DWORD dw1, DWORD dw2) {
|
|
|
|
#else
|
|
|
|
void stop_test(int a) {
|
|
|
|
#endif
|
|
|
|
run_test = false;
|
2002-05-21 23:11:09 +00:00
|
|
|
}
|