FoRUmS

a questionabout using the time.h library to get the execution time of the oftware program  

Page 1 / 2
 

my123
(@my123)
Eminent Member
Joined: 4 months ago
Posts: 43
25/05/2021 2:53 pm  

Dear Mr. Robert Owen, I want to use the time library to check the program running time, but after using the time library, I found that the program can't compile, I don't know what is wrong with it, can you give me some advice, thank you very much。

#if defined(D_NEXYS_A7)
#include <bsp_printf.h>
#include <bsp_mem_map.h>
#include <bsp_version.h>
#include <stdio.h>
#include <stdlib.h>
#include<math.h>
#include<time.h>
#else
PRE_COMPILED_MSG("no platform was defined")
#endif
#include <psp_api.h>

 

int main(void)
{
int i = 0x0003;
int t;

clock_t begin,end;

begin = clock();
i = 100*sin(i);
end = clock();

t = (end - begin);

return 0;


ReplyQuote
my123
(@my123)
Eminent Member
Joined: 4 months ago
Posts: 43
25/05/2021 2:55 pm  

The error


ReplyQuote
RCWO
 RCWO
(@rcwo)
Member Admin
Joined: 2 years ago
Posts: 22
25/05/2021 4:37 pm  

Greetings my123

We are very pleased to see you working through all of RVfpga.
We will take a look at your question and report back shortly.

R.


ReplyQuote
dchaver
(@dchaver)
Active Member
Joined: 7 months ago
Posts: 15
25/05/2021 8:04 pm  

Hi @my123

I do not think that the time library is implemented for SweRVolf; however, I'm going to double-check and let you know.

Meanwhile, you can use SweRV Hardware Counters, which we'll explain in detail in RVfpga Labs 11-20. Please find next a simple example for measuring cycles and seconds. You can change the empty for loop for whatever code you want to measure.

#if defined(D_NEXYS_A7)
#include <bsp_printf.h>
#include <bsp_mem_map.h>
#include <bsp_version.h>
#else
PRE_COMPILED_MSG("no platform was defined")
#endif
#include <psp_api.h>

int main(void)
{
int cyc_beg, cyc_end;
int i = 0;

/* Initialize Uart and Perf. Counters */
uartInit();
pspEnableAllPerformanceMonitor(1);

/* Measure Cycles */
pspPerformanceCounterSet(D_PSP_COUNTER0, E_CYCLES_CLOCKS_ACTIVE);

cyc_beg = pspPerformanceCounterGet(D_PSP_COUNTER0);
for(i=0;i<100000000;i++);
cyc_end = pspPerformanceCounterGet(D_PSP_COUNTER0);

printfNexys("Cycles = %d", cyc_end-cyc_beg);
printfNexys("Seconds = %d", (cyc_end-cyc_beg)/(50000000)); /* RVfpga Frequency = 50MHz */

while(1);
}

 

I hope this helps.

Dani


ReplyQuote
my123
(@my123)
Eminent Member
Joined: 4 months ago
Posts: 43
26/05/2021 2:54 pm  

Hi @dchaver

When I follow the C code you gave me, it does print out some values. The strange thing is that no matter how I change the value of i, the value of cyc_end-cyc_beg always seems to be 20. This is obviously a wrong counter.


ReplyQuote
dchaver
(@dchaver)
Active Member
Joined: 7 months ago
Posts: 15
26/05/2021 4:41 pm  

Hi @my123,

I'm not having the same behavior as you. When I change the value of i, the number of cycles changes proportionally.

Your problem could be caused by the compiler optimization. Given that the loop does nothing useful, your compiler could be removing the whole loop. Can you try the following solutions?

1. In PlatformIO you can change the optimization level (see: https://community.platformio.org/t/are-optimize-flags-possible/192). If you use -O0, the loop should not be removed.

2. You can also try a program that does useful job in the loop, so the compiler will never remove it:

#if defined(D_NEXYS_A7)
#include <bsp_printf.h>
#include <bsp_mem_map.h>
#include <bsp_version.h>
#else
PRE_COMPILED_MSG("no platform was defined")
#endif
#include <psp_api.h>

#include <stdlib.h>

int main(void)
{
int cyc_beg, cyc_end;
int i = 0, sum = 0;

/* Initialize Uart and Perf. Counters */
uartInit();
pspEnableAllPerformanceMonitor(1);

/* Measure Cycles */
pspPerformanceCounterSet(D_PSP_COUNTER0, E_CYCLES_CLOCKS_ACTIVE);

cyc_beg = pspPerformanceCounterGet(D_PSP_COUNTER0);
for(i=0;i<1000000;i++) sum = sum + rand()%10;
cyc_end = pspPerformanceCounterGet(D_PSP_COUNTER0);

printfNexys("Cycles = %d", cyc_end-cyc_beg);
printfNexys("Seconds = %d", (cyc_end-cyc_beg)/(50000000)); /* RVfpga Frequency = 50MHz */
printfNexys("Sum = %d", sum);

while(1);
}

The screenshot attached shows the result in my computer.

Note that the counter for seconds rounds the result to the lower integer, thus if it is small, it will show 0.

Let me know if you can solve the problem.

Best regards,

Dani


ReplyQuote
my123
(@my123)
Eminent Member
Joined: 4 months ago
Posts: 43
28/05/2021 2:49 pm  

Hi @dchaver

Yes, as you said, after using -O0, the counter runs normally, so I can know how long my program takes to run, thank you very much.

But a new problem appeared. I encountered a problem when using math.h library functions to process data. When I use the code in Figure 1 to run the program, the program can output the results normally. Because there is a large amount of data to be processed, I used the for statement in Figure 2 to perform calculations, but an error occurred and the compilation failed. However, the two codes are the same, which makes me very confused. Is this related to the settings of pio.ini

minyang


ReplyQuote
my123
(@my123)
Eminent Member
Joined: 4 months ago
Posts: 43
28/05/2021 2:51 pm  

figure 1


ReplyQuote
my123
(@my123)
Eminent Member
Joined: 4 months ago
Posts: 43
28/05/2021 2:55 pm  

figure 2


ReplyQuote
dchaver
(@dchaver)
Active Member
Joined: 7 months ago
Posts: 15
28/05/2021 8:20 pm  

Hi @my123,

You can find a good explanation here: https://stackoverflow.com/questions/3533594/sqrt-function-not-working-with-variable-arguments

For resolving it, you need to add to platformio.ini the following line: build_flags = -lm

See the screenshots.

Best regards


ReplyQuote
my123
(@my123)
Eminent Member
Joined: 4 months ago
Posts: 43
30/05/2021 2:13 pm  

Hi @dchaver

After I add build_flags = -lm to platformio.ini, I can put variables in sqrt(). But in some cases, I get strange number when I use functions like log() in the math.h library to calculate variables: 31 bits are all 1 (2147483647), I think it should be a configuration problem in the platformio.ini file.

I plan to use C language to implement a 256-point fft (fast Fourier transform), and calculate the log value of the frequency modulus H[i] (i from 0 to 255), that is, log(H[i]), but anyway, This value is always equal to 2147483647.

At first I thought that the value of H[i] was problematic, but by printing the value of H[i] to the screen through printfNexys(), I found that the value of H[i] was correct, which is the same as the result of matlab. Figure 1 shows the values ​​of H[0] and log(H[0]).

After excluding the problem of the value of H[i], I considered that it might be a problem with my algorithm, so I put the c code in the c compiler under windows to run, and found the value of log(H[0]) is normal, as shown in figure 2.

In the process of solving this problem, I did the following tests:
1. Define an array directly, initialize it, and log the elements in the array in a loop, and the calculation result is correct.
2. The result of log operation on the original time domain data x_r[i] of fft is wrong, that is, 31 ones.
3. After commenting out the fft algorithm, the result of the log operation on x_r[i] is correct.

This problem may be more complicated, but we have tried various methods and still cannot solve the problem of calculation errors. Taking the logarithm of the frequency modulus is an important step in the mfcc speech recognition algorithm, but now our project is stuck in the log calculation and cannot progress. , So I really need your help.

I think the problem should be the compiler problem or the SweRV kernel problem. I look forward to your reply. In addition, I will send you the source code via the link below.

minyang


ReplyQuote
my123
(@my123)
Eminent Member
Joined: 4 months ago
Posts: 43
30/05/2021 2:15 pm  

figure 1


ReplyQuote
my123
(@my123)
Eminent Member
Joined: 4 months ago
Posts: 43
30/05/2021 2:17 pm  

figure 2


ReplyQuote
my123
(@my123)
Eminent Member
Joined: 4 months ago
Posts: 43
30/05/2021 2:45 pm  

I have sent the source code fft.c to iup@imgtec.com through wetransfer.


ReplyQuote
my123
(@my123)
Eminent Member
Joined: 4 months ago
Posts: 43
03/06/2021 2:53 pm  

Hi @dchaver

So, is there any good solution to this problem?


ReplyQuote
Page 1 / 2