// Exp9 // -------------------------------------------------------------------------------- // TARGACEPT // Lee Atkinson // March 25, 2004 // -------------------------------------------------------------------------------- // -------------------------------------------------------------------------------- // EXP8: // This is a test program which // // // C++ with F90 Test // ----------------- // // 1st: // create a C++ main that call a // C++ function as though converted from fortran // and have the C++ function call a fortran function // // The above will illustrate the conversion from F90 // to C++ using non-converted F90 libs. // // Results should determine method and details of conversion // // // // 2nd: // For C++/ C++-F90/ F90: // // call a 2d array assignment, 1000 times // call a loop on 1000 to 2d array assignment, 1 times // need timer, use TNT Stopwatch // // Perform evaluation on straight calls to F90 // ------------------ // // -------------------------------------------------------------------------------- // -------------------------------------------------------------------------------- // INCLUDES // /usr/include/c++/3.2.2/ // -------------------------------------------------------------------------------- #include #include #include #include #include #include "tnt.h" using namespace TNT; using namespace blitz; // -------------------------------------------------------------------------------- // g++ -O3 -funroll-loops -frerun-loop-opt -o arytst9 exp9.cc arraytest.o -I/usr/include/c++/3.2.2/ -I/opt/tnt/1.2/src -I/opt/blitz-0.6 -ftemplate-depth-30 -L/opt/intel_fc_80/lib /opt/intel_fc_80/lib/libifcore.so.5 -L/usr/local/blitz-0.6/lib -lblitz -lm // -------------------------------------------------------------------------------- // -------------------------------------------------------------------------------- extern "C" { void init_2d_array_(int* M, int* N, int* thisarray); } // -------------------------------------------------------------------------------- // -------------------------------------------------------------------------------- // MYFUNCTION // -------------------------------------------------------------------------------- int test_one(int M, int N, int thisarray[1000][1000]) { // HYPOTHETICAL FORTRAN 90 // do i=1,10 // do j=1,10 // thisarray(j,i)=19 // end do // end do //C++ TRANSLATION // Note: Must argment bounds in C/C++ to avoid seg fault for (int i=0; i < M; i++) { for (int j=0; j < N; j++) { //thisarray[j][i]=19; thisarray[j][i]=(i*N)+j; } } } int test_two(int M, int N, Fortran_Array2D thisarray) { // HYPOTHETICAL FORTRAN 90 // do i=1,10 // do j=1,10 // thisarray(j,i)=19 // end do // end do //C++ TRANSLATION for (int i=1; i <= M; i++) { for (int j=1; j <= N; j++) { //thisarray(j,i)=19; thisarray(j,i)=(i*N)+j; } } } // Test Three is an inline Fortran call to init_2d_array. See MAIN int test_four(int M, int N, int thisarray[1000][1000]) { // NOTE: Switching iteration order or subscript order for C++ optimization for (int i=0; i < M; i++) { for (int j=0; j < N; j++) { //thisarray[i][j]=19; thisarray[i][j]=(i*N)+j; } } } // Variable Array test using TNT: Array2D A(M,N); int test_five(int M, int N, Array2D thisarray) { // NOTE: Switching iteration order or subscript order for C++ optimization for (int i=0; i < M; i++) { for (int j=0; j < N; j++) { //thisarray[i][j]=19; thisarray[i][j]=(i*N)+j; } } } // Variable Array test using Bliltz++: Array int test_six(int M, int N, Array thisarray) { // NOTE: Switching iteration order or subscript order for C++ optimization for (int i=0; i < M; i++) { for (int j=0; j < N; j++) { //thisarray[i][j]=19; thisarray[i][j]=(i*N)+j; } } } // -------------------------------------------------------------------------------- // MAIN // -------------------------------------------------------------------------------- int main () { using namespace std; int steps=10000; int M=1000; int N=1000; int ii,jj; Stopwatch Q; double time_elapsed; int R[1000][1000]; int *Rptr; int* Mptr; int* Nptr; Array2D A(M,N); /* create C++ TNT MxN array; all zeros */ Fortran_Array2D F(M,N); /* create TNT Fortran MxN array; all zeros */ Array B(M,N); /* create C++ Blitz++ MxN array; all zeros */ Mptr=&M; Nptr=&N; Rptr=&R[0][0]; cout << "Hello TARGACEPT\n"; printf("MAIN: \n\n"); printf("TEST ONE - SIMULATED C++ CONVERSION of F90 SUBROUTINE CALL\n"); printf("TEST ONE - %d invocations of function test_one\n", steps); Q.start(); for (int k=0; k < steps; k++) { test_one(M,N,R); } Q.stop(); time_elapsed = Q.read(); printf("elapsed time in clock ticks: %f\n\n", time_elapsed); printf("TEST TWO - SIMULATED C++ CONVERSION of F90 SUBROUTINE CALL using TNT F90 Array\n"); printf("TEST TWO - %d invocations of function test_two\n", steps); Q.start(); for (int k=0; k < steps; k++) { test_two(M,N,F); } Q.stop(); time_elapsed = Q.read(); printf("elapsed time in clock ticks: %f\n\n", time_elapsed); printf("TEST THREE - C++ Call of F90 SUBROUTINE CALL\n"); printf("TEST THREE - %d invocations of function test_three\n", steps); Q.start(); for (int k=0; k < steps; k++) { init_2d_array_(Mptr, Nptr, Rptr); } Q.stop(); time_elapsed = Q.read(); printf("elapsed time in clock ticks: %f\n\n", time_elapsed); printf("TEST FOUR - C++ Call of COMPLETE C++\n"); printf("TEST FOUR - %d invocations of function test_four\n", steps); Q.start(); for (int k=0; k < steps; k++) { test_four(M, N, R); } Q.stop(); time_elapsed = Q.read(); printf("elapsed time in clock ticks: %f\n\n", time_elapsed); printf("TEST FIVE - C++ Call of COMPLETE C++ (variable array)\n"); printf("TEST FIVE - %d invocations of function test_five\n", steps); Q.start(); for (int k=0; k < steps; k++) { test_five(M, N, A ); } Q.stop(); time_elapsed = Q.read(); printf("elapsed time in clock ticks: %f\n\n", time_elapsed); //printf("I=17 J=23 I*N+J=17,023 A[17][23]=%d\n", A[17][23]); printf("TEST SIX - Blitz++/C++ Call of COMPLETE C++ (variable array)\n"); printf("TEST SIX - %d invocations of function test_six\n", 100); Q.start(); for (int k=0; k < 100; k++) { test_six(M, N, B ); } Q.stop(); time_elapsed = Q.read(); printf("elapsed time in clock ticks: %f\n\n", time_elapsed); cout << "Bye TARGACEPT\n"; }