Advanced Computing Platform for Theoretical Physics

Commit 70732d4e authored by Mengzhi Han's avatar Mengzhi Han
Browse files

cpuSDK initial commit

parents
/*** For time statistics ***/
#ifndef PPPTIMES_H
#define PPPTIMES_H
#include <sys/time.h>
#include <sys/times.h>
#include <unistd.h>
/*********get the total time for computation***********/
double gettime()
{
static int startflag=1;
struct timeval tv;
struct timezone tz;
static double tsecs0;
double tsecs1, dt;
if( startflag )
{
(void) gettimeofday(&tv,&tz);
tsecs0 = tv.tv_sec + tv.tv_usec*1.0e-6;
startflag = 0;
}
(void) gettimeofday(&tv,&tz);
tsecs1 = tv.tv_sec + tv.tv_usec*1.0e-6;
dt = tsecs1-tsecs0;
tsecs0 = tsecs1;
if( dt <= 0 )
dt = 0;
return dt;
}
/*********compute the wall clock time ***********/
double wallclocktime()
{
static int startflag=1;
struct timeval tv;
struct timezone tz;
static double tsecs0;
double tsecs1, dt;
if( startflag )
{
(void) gettimeofday(&tv,&tz);
tsecs0 = tv.tv_sec + tv.tv_usec*1.0e-6;
startflag = 0;
}
(void) gettimeofday(&tv,&tz);
tsecs1 = tv.tv_sec + tv.tv_usec*1.0e-6; //΢תΪ
dt = tsecs1-tsecs0;
tsecs0 = tsecs1;
if( dt <= 0 )
dt = 0;
return dt;
}
#endif
Some commonly used CPU timing functions, include:
clock
times
gettimeofday
clock_gettime
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main( void )
{
long i=1000L;
clock_t start, finish;
double duration;
printf("CLOCKS_PER_SEC is %ld\n", CLOCKS_PER_SEC);
printf( "Time to do %ld empty loops is ", i );
start = clock();
while (--i){
system("cd");
}
finish = clock();
duration = (double)(finish - start) / CLOCKS_PER_SEC;
printf( "%f seconds\n", duration );
return 0;
}
#include<time.h>
#include<stdio.h>
#include<stdlib.h>
#define MILLION 1000000
int main(void)
{
long int loop = 1000;
struct timespec tpstart;
struct timespec tpend;
long timedif;
clock_gettime(CLOCK_MONOTONIC, &tpstart);
while (--loop){
system("cd");
}
clock_gettime(CLOCK_MONOTONIC, &tpend);
timedif = MILLION*(tpend.tv_sec-tpstart.tv_sec)+(tpend.tv_nsec-tpstart.tv_nsec)/1000;
fprintf(stdout, "it took %ld microseconds\n", timedif);
return 0;
}
#include <sys/time.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
long int loop = 1000;
struct timeval tvs,tve;
gettimeofday(&tvs,NULL);
while (--loop){
system("cd");
}
gettimeofday(&tve,NULL);
double span = tve.tv_sec-tvs.tv_sec + (tve.tv_usec-tvs.tv_usec)/1000000.0;
printf("time: %.12f\n",span);
return 0;
}
clock1: clock1.c
gcc clock1.c -o clock1
times1: times1.c
gcc times1.c -o times1
clock_gettime1: clock_gettime1.c
gcc clock_gettime1.c -o clock_gettime1
gettimeofday1: gettimeofday1.c
gcc gettimeofday1.c -o gettimeofday1
test4: test4.c
gcc test4.c -o test4
test5: test5.c
gcc test5.c -o test5
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <time.h>
#include <sys/times.h>
#include <sys/time.h>
#define WAIT for(i=0;i<298765432;i++);
#define MILLION 1000000
int main (int argc, char *argv[] )
{
int i;
long ttt;
clock_t s,e;
struct tms aaa;
s=clock();
WAIT;
e=clock();
printf("clock time : %.12f\n",(e-s)/(double)CLOCKS_PER_SEC);
long tps = sysconf(_SC_CLK_TCK);
s=times(&aaa);
WAIT;
e=times(&aaa);
printf("times time : %.12f\n",(e-s)/(double)tps);
struct timeval tvs,tve;
gettimeofday(&tvs,NULL);
WAIT;
gettimeofday(&tve,NULL);
double span = tve.tv_sec-tvs.tv_sec + (tve.tv_usec-tvs.tv_usec)/1000000.0;
printf("gettimeofday time: %.12f\n",span);
struct timespec tpstart;
struct timespec tpend;
clock_gettime(CLOCK_REALTIME, &tpstart);
WAIT;
clock_gettime(CLOCK_REALTIME, &tpend);
double timedif = (tpend.tv_sec-tpstart.tv_sec)+(tpend.tv_nsec-tpstart.tv_nsec)/1000000000.0;
printf("clock_gettime time: %.12f\n", timedif);
return EXIT_SUCCESS;
}
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <time.h>
#include <sys/times.h>
#include <sys/time.h>
#define MILLION 1000000
void wait()
{
long loop = 1000L;
while(--loop)
{
system("cd");
}
}
int main (int argc, char *argv[] )
{
int i;
long ttt;
clock_t s,e;
struct tms aaa;
s=clock();
wait();
e=clock();
printf("clock time : %.12f\n",(e-s)/(double)CLOCKS_PER_SEC);
long tps = sysconf(_SC_CLK_TCK);
s=times(&aaa);
wait();
e=times(&aaa);
printf("times time : %.12f\n",(e-s)/(double)tps);
struct timeval tvs,tve;
gettimeofday(&tvs,NULL);
wait();
gettimeofday(&tve,NULL);
double span = tve.tv_sec-tvs.tv_sec + (tve.tv_usec-tvs.tv_usec)/1000000.0;
printf("gettimeofday time: %.12f\n",span);
struct timespec tpstart;
struct timespec tpend;
clock_gettime(CLOCK_REALTIME, &tpstart);
wait();
clock_gettime(CLOCK_REALTIME, &tpend);
double timedif = (tpend.tv_sec-tpstart.tv_sec)+(tpend.tv_nsec-tpstart.tv_nsec)/1000000000.0;
printf("clock_gettime time: %.12f\n", timedif);
return EXIT_SUCCESS;
}
#include <sys/times.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
static void do_cmd(char *);
static void pr_times(clock_t, struct tms *, struct tms *);
int main(int argc, char *argv[]){
int i;
for(i=1; argv[i]!=NULL; i++){
do_cmd(argv[i]);
}
exit(1);
}
static void do_cmd(char *cmd){
struct tms tmsstart, tmsend;
clock_t start, end;
int status;
if((start=times(&tmsstart))== -1)
puts("times error");
if((status=system(cmd))<0)
puts("system error");
if((end=times(&tmsend))== -1)
puts("times error");
pr_times(end-start, &tmsstart, &tmsend);
exit(0);
}
static void pr_times(clock_t real, struct tms *tmsstart, struct tms *tmsend){
static long clktck=0;
if(0 == clktck)
if((clktck=sysconf(_SC_CLK_TCK))<0)
puts("sysconf err");
printf("real:%7.2f\n", real/(double)clktck);
printf("user-cpu:%7.2f\n", (tmsend->tms_utime - tmsstart->tms_utime)/(double)clktck);
printf("system-cpu:%7.2f\n", (tmsend->tms_stime - tmsstart->tms_stime)/(double)clktck);
printf("child-user-cpu:%7.2f\n", (tmsend->tms_cutime - tmsstart->tms_cutime)/(double)clktck);
printf("child-system-cpu:%7.2f\n", (tmsend->tms_cstime - tmsstart->tms_cstime)/(double)clktck);
}
A lapack dgesv example, compile with reference lapack and MKL
Not use c interface, direct link to the gfortran library.
function name in library: dgesv -> dgesv_
add compile option: -Ddgesv=dgesv_
/*
From Intel Math Kernel Library LAPACK Examples:
https://software.intel.com/sites/products/documentation/doclib/mkl_sa/11/mkl_lapack_examples/index.htm
DGESV Example.
==============
The program computes the solution to the system of linear
equations with a square matrix A and multiple
right-hand sides B, where A is the coefficient matrix:
6.80 -6.05 -0.45 8.32 -9.67
-2.11 -3.30 2.58 2.71 -5.14
5.66 5.36 -2.70 4.35 -7.26
5.97 -4.44 0.27 -7.17 6.08
8.23 1.08 9.04 2.14 -6.87
and B is the right-hand side matrix:
4.02 -1.56 9.81
6.19 4.00 -4.09
-8.22 -8.67 -4.57
-7.57 1.75 -8.61
-3.03 2.86 8.99
Description.
============
The routine solves for X the system of linear equations A*X = B,
where A is an n-by-n matrix, the columns of matrix B are individual
right-hand sides, and the columns of X are the corresponding
solutions.
The LU decomposition with partial pivoting and row interchanges is
used to factor A as A = P*L*U, where P is a permutation matrix, L
is unit lower triangular, and U is upper triangular. The factored
form of A is then used to solve the system of equations A*X = B.
Example Program Results.
========================
DGESV Example Program Results
Solution
-0.80 -0.39 0.96
-0.70 -0.55 0.22
0.59 0.84 1.90
1.32 -0.10 5.36
0.57 0.11 4.04
Details of LU factorization
8.23 1.08 9.04 2.14 -6.87
0.83 -6.94 -7.92 6.55 -3.99
0.69 -0.67 -14.18 7.24 -5.19
0.73 0.75 0.02 -13.82 14.19
-0.26 0.44 -0.59 -0.34 -3.43
Pivot indices
5 5 3 4 5
*/
#include <stdlib.h>
#include <stdio.h>
/* DGESV prototype */
extern void dgesv( int* n, int* nrhs, double* a, int* lda, int* ipiv,
double* b, int* ldb, int* info );
/* Auxiliary routines prototypes */
extern void print_matrix( char* desc, int m, int n, double* a, int lda );
extern void print_int_vector( char* desc, int n, int* a );
/* Parameters */
#define N 5
#define NRHS 3
#define LDA N
#define LDB N
/* Main program */
int main() {
/* Locals */
int n = N, nrhs = NRHS, lda = LDA, ldb = LDB, info;
/* Local arrays */
int ipiv[N];
double a[LDA*N] = {
6.80, -2.11, 5.66, 5.97, 8.23,
-6.05, -3.30, 5.36, -4.44, 1.08,
-0.45, 2.58, -2.70, 0.27, 9.04,
8.32, 2.71, 4.35, -7.17, 2.14,
-9.67, -5.14, -7.26, 6.08, -6.87
};
double b[LDB*NRHS] = {
4.02, 6.19, -8.22, -7.57, -3.03,
-1.56, 4.00, -8.67, 1.75, 2.86,
9.81, -4.09, -4.57, -8.61, 8.99
};
/* Executable statements */
printf( " DGESV Example Program Results\n" );
/* Solve the equations A*X = B */
dgesv( &n, &nrhs, a, &lda, ipiv, b, &ldb, &info );
/* Check for the exact singularity */
if( info > 0 ) {
printf( "The diagonal element of the triangular factor of A,\n" );
printf( "U(%i,%i) is zero, so that A is singular;\n", info, info );
printf( "the solution could not be computed.\n" );
exit( 1 );
}
/* Print solution */
print_matrix( "Solution", n, nrhs, b, ldb );
/* Print details of LU factorization */
print_matrix( "Details of LU factorization", n, n, a, lda );
/* Print pivot indices */
print_int_vector( "Pivot indices", n, ipiv );
exit( 0 );
} /* End of DGESV Example */
/* Auxiliary routine: printing a matrix */
void print_matrix( char* desc, int m, int n, double* a, int lda ) {
int i, j;
printf( "\n %s\n", desc );
for( i = 0; i < m; i++ ) {
for( j = 0; j < n; j++ ) printf( " %6.2f", a[i+j*lda] );
printf( "\n" );
}
}
/* Auxiliary routine: printing a vector of integers */
void print_int_vector( char* desc, int n, int* a ) {
int j;
printf( "\n %s\n", desc );
for( j = 0; j < n; j++ ) printf( " %6i", a[j] );
printf( "\n" );
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment