Intel® C++ Compiler
Community support and assistance for creating C++ code that runs on platforms based on Intel® processors.

problem with finding time

Sharada_Kulkarni
Beginner
1,774 Views

int main()
{ clock_t start,stop;
double startTime,endTime;

startTime=omp_get_wtime( );
start =clock();
printf("\nn=%d,\nstart time=%10.9f\n",n,((double)(start)/CLOCKS_PER_SEC));

quicksort(a,0,n-1);

stop=clock();

endTime=omp_get_wtime( );
printf("Elapsed time=%10.9f",(endTime-startTime));
printf("\nstop=%10.9f\n",(((double)(stop))/CLOCKS_PER_SEC));
printf("Time taken :%10.9f sec",((double)(stop-start)/CLOCKS_PER_SEC));
}

void quicksort(int a[],int lb,int ub)
{
int i,j,temp,key,flag=1;
if ( lb < ub )
{
i = lb;
j = ub + 1;
key = a [ lb ];
while ( flag )
{ i++;
while(a < key && i < ub )
i++;
j--;
while ( a [ j ] > key)
j--;
if ( i < j )
{
temp = a;
a = a;
a = temp;
}
else
flag = 0;
}
a[lb]=a;
a=key;
#pragma omp parallel shared(a,j)
{
#pragma omp parallel sections
{
#pragma omp section
{ if(lb<(j-1))
quicksort(a,lb,j-1);
}
#pragma omp section
{ if ((j+1) quicksort(a,j+1,ub);
}
}
}
}
}

Even when itestwith 1100 numbers there is no difference in the start and stop time(I am working on dual core machine). Please can anybody tell me why .
Which time functions I must use for serial codes to find elapsed time.(I am using intel C++)


0 Kudos
1 Solution
Om_S_Intel
Employee
1,774 Views

The clock() timer is working without any issue. I have used the following code to test it out:

// tstcase.c : Defines the entry point for the console application.
//

#include
#include
#define N 10000000
#define M 10

float a, b, c;

int main()
{
int i;
clock_t start,stop;
double startTime,endTime;

startTime=omp_get_wtime( );
start =clock();
//printf("start=%10.9fn",((double)(start)/CLOCKS_PER_SEC));
//printf("start time=%10.9fn",startTime);

for (i = 0; i < N; ++i)
{
a= i;
b= i;
c = i;
}
for (i = 0; i < N; ++i)
a= b + c;

for (i = 0; i < N; ++i)
a= b + c;

for (i = 0; i < N; ++i)
a= b + c;

for (i = 0; i < N; ++i)
a= b + c;

for (i = 0; i < N; ++i)
a= b + c;


stop=clock();

endTime=omp_get_wtime( );

//printf("end time=%10.9fn",endTime);
printf("Elapsed time (openmp timer)=%10.9fn",(endTime-startTime));
//printf("stop=%10.9fn",(((float)(stop))/CLOCKS_PER_SEC));
printf("Time taken (clock() timer) :%10.9f secn",((float)(stop-start)/CLOCKS_PER_SEC));

return 0;
}


C:>icl /Qopenmp tstcase.c
Intel C++ Compiler for applications running on IA-32, Version 11.1 Build 2
0090421 Package ID: composer.061
Copyright (C) 1985-2009 Intel Corporation. All rights reserved.

tstcase.c
Microsoft Incremental Linker Version 9.00.21022.08
Copyright (C) Microsoft Corporation. All rights reserved.

-out:tstcase.exe
-nodefaultlib:libiompprof5mt.lib
-nodefaultlib:libiompprof5md.lib
-defaultlib:libiomp5md.lib
-nodefaultlib:vcomp.lib
-nodefaultlib:vcompd.lib
tstcase.obj

C:>tstcase.exe
Elapsed time (openmp timer)=0.220298669
Time taken (clock() timer) :0.217999995 sec

View solution in original post

0 Kudos
5 Replies
TimP
Honored Contributor III
1,774 Views
If you use standard or openmp library functions without including the headers, you can't expect them to work. Can you take the advice of your favorite compiler? If you have a compiler which allows you to run code with so many errors, remind us not to use it for learning by trial and error. Surely it's not Intel C.

icc -openmp -diag-enable sc sk.c
sk.c(4): error: identifier "clock_t" is undefined
clock_t start, stop;
^

sk.c(9): error: identifier "n" is undefined
printf ("nn=%d,nstart time=%10.9fn", n,
^

sk.c(10): error: identifier "CLOCKS_PER_SEC" is undefined
((double) (start) / CLOCKS_PER_SEC));
^


sk.c(12): error: identifier "a" is undefined
quicksort (a, 0, n - 1);
^

sk.c(24): warning #159: declaration is incompatible with previous "quicksort" (declared at line 12)
quicksort (int a[], int lb, int ub)
^

compilation aborted for sk.c (code 2)
icc: warning #10281: source checker not called due to source compilation problem, please resolve before using the source checker



gcc sk.c
sk.c: In function main:
sk.c:4: error: clock_t undeclared (first use in this function)
sk.c:4: error: (Each undeclared identifier is reported only once
sk.c:4: error: for each function it appears in.)
sk.c:4: error: expected ; before start
sk.c:8: error: start undeclared (first use in this function)
sk.c:9: warning: incompatible implicit declaration of built-in function printf
sk.c:9: error: n undeclared (first use in this function)
sk.c:10: error: CLOCKS_PER_SEC undeclared (first use in this function)
sk.c:12: error: a undeclared (first use in this function)
sk.c:14: error: stop undeclared (first use in this function)
sk.c: At top level:
sk.c:24: warning: conflicting types for quicksort
sk.c:12: note: previous implicit declaration of quicksort was here

0 Kudos
Sharada_Kulkarni
Beginner
1,774 Views
Quoting - tim18
If you use standard or openmp library functions without including the headers, you can't expect them to work. Can you take the advice of your favorite compiler? If you have a compiler which allows you to run code with so many errors, remind us not to use it for learning by trial and error. Surely it's not Intel C.

icc -openmp -diag-enable sc sk.c
sk.c(4): error: identifier "clock_t" is undefined
clock_t start, stop;
^

sk.c(9): error: identifier "n" is undefined
printf ("nn=%d,nstart time=%10.9fn", n,
^

sk.c(10): error: identifier "CLOCKS_PER_SEC" is undefined
((double) (start) / CLOCKS_PER_SEC));
^


sk.c(12): error: identifier "a" is undefined
quicksort (a, 0, n - 1);
^

sk.c(24): warning #159: declaration is incompatible with previous "quicksort" (declared at line 12)
quicksort (int a[], int lb, int ub)
^

compilation aborted for sk.c (code 2)
icc: warning #10281: source checker not called due to source compilation problem, please resolve before using the source checker



gcc sk.c
sk.c: In function main:
sk.c:4: error: clock_t undeclared (first use in this function)
sk.c:4: error: (Each undeclared identifier is reported only once
sk.c:4: error: for each function it appears in.)
sk.c:4: error: expected ; before start
sk.c:8: error: start undeclared (first use in this function)
sk.c:9: warning: incompatible implicit declaration of built-in function printf
sk.c:9: error: n undeclared (first use in this function)
sk.c:10: error: CLOCKS_PER_SEC undeclared (first use in this function)
sk.c:12: error: a undeclared (first use in this function)
sk.c:14: error: stop undeclared (first use in this function)
sk.c: At top level:
sk.c:24: warning: conflicting types for quicksort
sk.c:12: note: previous implicit declaration of quicksort was here


Oh Sorry ! I had not included the full code.Anyway thanks a lot for looking into it.here is the comlete code to test with
icl /Qopenmp test.c

#include
#include
#include
#include
#define MAX 10000

void qsort(void);
void quicksort(int a[],int lo,int hi);

int main()
{

FILE *fp;
int i,n=0,j;
int a[MAX];
clock_t start,stop;
char ar[MAX];double startTime,endTime;

printf("Enter the file name n");
scanf("%s",ar);
if((fp=fopen(ar,"r"))==NULL)
{
printf("Can't open file.n");
exit(1);
}
i=fscanf(fp,"%d",&a);
while(i != EOF)
{
n++;
i=fscanf(fp,"%d",&a);
}
fclose(fp);

startTime = omp_get_wtime( );
start = clock( );
printf("nn=%d,nstart time=%10.9fn",n,(((double)(start))/CLOCKS_PER_SEC));
quicksort(a,0,n-1);
stop = clock();
endTime=omp_get_wtime( );

printf("Elapsed time=%10.5f",endTime-startTime);
printf("nstop=%10.9fn",(((double)(stop))/CLOCKS_PER_SEC));
printf("Time taken :%10.9f sec",(((double)(stop-start))/CLOCKS_PER_SEC));
}

void quicksort(int a[],int lb,int ub)
{
int i,j,temp,key,flag=1;

if ( lb < ub )
{
i = lb;
j = ub + 1;
key = a [ lb ];
while ( flag )
{ i++;
while(a < key && i < ub )
i++;
j--;
while ( a [ j ] > key)
j--;
if ( i < j )
{
temp = a;
a = a;
a = temp;
}
else
flag = 0;
}
a[lb]=a;
a=key;
#pragma omp parallel sections
{
#pragma omp section
{ if(lb<(j-1))
quicksort(a,lb,j-1);
}
#pragma omp section
{ if ((j+1) quicksort(a,j+1,ub);
}
}
}
}



0 Kudos
Dale_S_Intel
Employee
1,774 Views
I assume you're talking about this problem:

[cpp]$ cat bug2.c 

#include 
#include 

int main(int argc, char *argv[])
{
   clock_t start,stop;
   double startTime,endTime;

startTime=omp_get_wtime( );
start =clock();
printf("start=%10.9fn",((double)(start)/CLOCKS_PER_SEC));
printf("start time=%10.9fn",startTime);

sleep(10);

stop=clock();

endTime=omp_get_wtime( );

printf("end time=%10.9fn",endTime);
printf("Elapsed time=%10.9fn",(endTime-startTime));
printf("stop=%10.9fn",(((double)(stop))/CLOCKS_PER_SEC));
printf("Time taken :%10.9f secn",((double)(stop-start)/CLOCKS_PER_SEC));

}

$ icc -openmp bug2.c
$ ./a.out 
start=0.000000000
start time=1242761092.957634926
end time=1242761102.958846092
Elapsed time=10.001211166
stop=0.000000000
Time taken :0.000000000 sec
$ 
[/cpp]


This does seem to work as expected on Windows but not on Linux or Mac, as far as my investigations so far indicate. What platform are you running?

Thanks!

Dale

0 Kudos
Sharada_Kulkarni
Beginner
1,774 Views
I assume you're talking about this problem:

[cpp]$ cat bug2.c 

#include 
#include 

int main(int argc, char *argv[])
{
   clock_t start,stop;
   double startTime,endTime;

startTime=omp_get_wtime( );
start =clock();
printf("start=%10.9fn",((double)(start)/CLOCKS_PER_SEC));
printf("start time=%10.9fn",startTime);

sleep(10);

stop=clock();

endTime=omp_get_wtime( );

printf("end time=%10.9fn",endTime);
printf("Elapsed time=%10.9fn",(endTime-startTime));
printf("stop=%10.9fn",(((double)(stop))/CLOCKS_PER_SEC));
printf("Time taken :%10.9f secn",((double)(stop-start)/CLOCKS_PER_SEC));

}

$ icc -openmp bug2.c
$ ./a.out 
start=0.000000000
start time=1242761092.957634926
end time=1242761102.958846092
Elapsed time=10.001211166
stop=0.000000000
Time taken :0.000000000 sec
$ 
[/cpp]


This does seem to work as expected on Windows but not on Linux or Mac, as far as my investigations so far indicate. What platform are you running?

Thanks!

Dale

HI ! thanks. i am using intel C++ on windows platform (icl /Qopenmp test.c)
I am not able to find elapsed time for serial code(CLOCK() functions are not working).
Can you please suggest me some functions.I use omp_get_wtime() for parallel code as i have done.


0 Kudos
Om_S_Intel
Employee
1,775 Views

The clock() timer is working without any issue. I have used the following code to test it out:

// tstcase.c : Defines the entry point for the console application.
//

#include
#include
#define N 10000000
#define M 10

float a, b, c;

int main()
{
int i;
clock_t start,stop;
double startTime,endTime;

startTime=omp_get_wtime( );
start =clock();
//printf("start=%10.9fn",((double)(start)/CLOCKS_PER_SEC));
//printf("start time=%10.9fn",startTime);

for (i = 0; i < N; ++i)
{
a= i;
b= i;
c = i;
}
for (i = 0; i < N; ++i)
a= b + c;

for (i = 0; i < N; ++i)
a= b + c;

for (i = 0; i < N; ++i)
a= b + c;

for (i = 0; i < N; ++i)
a= b + c;

for (i = 0; i < N; ++i)
a= b + c;


stop=clock();

endTime=omp_get_wtime( );

//printf("end time=%10.9fn",endTime);
printf("Elapsed time (openmp timer)=%10.9fn",(endTime-startTime));
//printf("stop=%10.9fn",(((float)(stop))/CLOCKS_PER_SEC));
printf("Time taken (clock() timer) :%10.9f secn",((float)(stop-start)/CLOCKS_PER_SEC));

return 0;
}


C:>icl /Qopenmp tstcase.c
Intel C++ Compiler for applications running on IA-32, Version 11.1 Build 2
0090421 Package ID: composer.061
Copyright (C) 1985-2009 Intel Corporation. All rights reserved.

tstcase.c
Microsoft Incremental Linker Version 9.00.21022.08
Copyright (C) Microsoft Corporation. All rights reserved.

-out:tstcase.exe
-nodefaultlib:libiompprof5mt.lib
-nodefaultlib:libiompprof5md.lib
-defaultlib:libiomp5md.lib
-nodefaultlib:vcomp.lib
-nodefaultlib:vcompd.lib
tstcase.obj

C:>tstcase.exe
Elapsed time (openmp timer)=0.220298669
Time taken (clock() timer) :0.217999995 sec
0 Kudos
Reply