0

So I've been attempting to get the runtime for a function in my code using the I'm wondering why my code isn't properly counting the time it takes though, since it is returning 0 seconds for a runtime when it shouldn't. Some possibilities I thought of was that I possibly messed up the 2D array, but if that's the case, shouldn't it return a null pointer?

EDIT: Original code N=500 should produce a runtime of around 1 second from my testing

#include <stdio.h>
#include <sys/time.h>

void matrixMultiplyRow(int N, int matrixA[][N], int matrixB[][N], int final[][N]); 

int main(){
    int N = 500;
    int output[N][N];
    int output2[N][N];
    int numElements = N*N;
    int i, j;
    int counter = 1;
    //Array 1 fills it with sequential numbers
    int experimentA[N][N];
    int experimentB[N][N];
    for(i=1; i<N+1; i++){
        for(j=1; j<N+1; j++){
            experimentA[i][j] = counter;
            experimentB[i][j] = counter;
            counter++;
        }
    }
    struct timeval start_time, stop_time, elapsed_time;
    gettimeofday(&start_time,NULL);
    matrixMultiplyRow(N, experimentA, experimentB, output);
    gettimeofday(&stop_time,NULL);
    timersub(&stop_time, &start_time, &elapsed_time); 
    printf("Total time was %f sec for Row Major.\n", elapsed_time.tv_sec+elapsed_time.tv_usec/1000000.0);
    fflush(stdout);
    return 1;
}

//Row Major 
void matrixMultiplyRow(int N, int matrixA[][N], int matrixB[][N], int final[][N]){
    int i, j, k;
    for(i=0; i<N; i++){
        for(j=0; j<N; j++){
            final[i][j] = 0;
            for(k=0; k<N; k++){
                final[i][j] += matrixA[i][k]*matrixB[k][j];
            }
        }
    }
}
Brain Meme
  • 61
  • 6
  • Please format code carefully. – user202729 Feb 09 '18 at 14:17
  • 2
    How do you expect us to reproduce the issue, in order to help diagnose and solve the issue, if you've not given us code to reproduce the issue? There's no calls to `gettimeofday` at all here? No `main` entry point? Read [this page on MCVEs](https://stackoverflow.com/help/mcve), **CAREFULLY**! In fact, read it twice, because we often have to tell people that their "MCVE" still is not an MCVE. – autistic Feb 09 '18 at 14:18
  • You might lack some additional tag, perhaps *Linux* or *POSIX*. Check in [n1570](http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1570.pdf) -which is the C11 standard specification-, you won't find `gettimeofday` mentioned in the C11 standard. On Linux, read also [time(7)](http://man7.org/linux/man-pages/man7/time.7.html) – Basile Starynkevitch Feb 09 '18 at 14:19
  • @Sebivor ... actually there is `gettimeofday` call, just no `main`. – user202729 Feb 09 '18 at 14:19
  • @user202729 Well, okay, nonetheless, if we attempt to compile and run this... will what we see match the symptoms described in the question? – autistic Feb 09 '18 at 14:20
  • 2
    How much wall time do you *think* elapsed, approximately? – John Bollinger Feb 09 '18 at 14:20
  • 1
    But an [MCVE] should be a complete program, so has some `main`. Your question don't have any `main` – Basile Starynkevitch Feb 09 '18 at 14:20
  • 2
    BTW, computers are pretty fast, several billions of operations per second. Have you run a program so that the `final[i][j] += matrixA[i][k]*matrixB[k][j];` statement is running *several billion times* ? Have you used [time(1)](http://man7.org/linux/man-pages/man1/time.1.html) on your program? – Basile Starynkevitch Feb 09 '18 at 14:21
  • It's not responsible for your issue, but don't you think it's a bit excessive to map three separate shared memory segments instead just one that's big enough for all the objects you want to share? Are you running up against some kind of limit? – John Bollinger Feb 09 '18 at 14:24
  • ... and why are you rolling your own *profiler* using `gettimeofday` to begin with? Use `gprof` to measure the most significant bottleneck. Just trying to help you come to the best solution... which involves... using the most appropriate tool for the job... – autistic Feb 09 '18 at 14:25
  • I left out a piece of code that controls the threads so it should work for one thread if that's necessary though, I will post it. I've tried doing N=1000 (Size of the square matrix) which gave me at least a few seconds before, but now, it is at 0. I also tried upping the N size, but it gave me a seg fault. Not sure how to edit the post, but the code above is the main function. – Brain Meme Feb 09 '18 at 14:25
  • 1
    Wait, threads? At this point, I think we would all be best served if you convert the code presented to a [mcve]. – John Bollinger Feb 09 '18 at 14:27
  • To edit the question, just click the [edit] button. – user202729 Feb 09 '18 at 14:28
  • And you do know, do you not, that various flavors of "shared memory segments" are inter-*process* communication mechanisms, right? You don't need them for different threads of the same process to share memory. – John Bollinger Feb 09 '18 at 14:30
  • @JohnBollinger you mean simply mapping it with a size of N*N, but using one shm_open? since it's three different arrays, I wasn't sure how to do it. would you have an example? since I'm kinda new to using this. – Brain Meme Feb 09 '18 at 14:30
  • This is all assuming it's a *problem* that your code is taking 0 seconds. If you're concerned about a segfault, you should *ask about that* (and provide code which reproduces it, in the form of ... you guessed it), rather than what could for all we know, be the correct behaviour. – autistic Feb 09 '18 at 14:30
  • oops @JohnBollinger I mean different processes since it's fork() – Brain Meme Feb 09 '18 at 14:31
  • Again: present an MCVE. – John Bollinger Feb 09 '18 at 14:31
  • You might also get some good advice from [this unrelated video](https://www.youtube.com/watch?v=jDB4mqTxcmo). It addresses numerous common problems that automotive engineering shares with software engineering, namely for you, the "non-issue" problem of your program taking less time than you expect should not necessarily be "fixed"... – autistic Feb 09 '18 at 14:36
  • @JohnBollinger I hope the code below works as MCVE? Sorry I am new to all of this – Brain Meme Feb 09 '18 at 14:42
  • You can test whether it's an MCVE yourself. Is it: 1/ minimal? Clearly not, as you could EASILY further reduce your code without changing the behaviour. For example, change `char *a = argv[1]; int N = atoi(a);` to `int N = 1000;`, if you intend to demonstrate the behaviour when N is 1000. Please continue along those lines until you have something that you think is *minimal*, and then show us, and we'll help you cover the other criteria: 2/ complete, 3/ verifiable and 4/ example – autistic Feb 09 '18 at 14:52
  • 3
    You know, there's a reason I asked you to read it twice... as I wrote, we often have to point out that peoples "MCVE"s aren't MCVEs. Less guessing, *much* more reading, would be fantastic... – autistic Feb 09 '18 at 14:53
  • 2
    ... and the "complete" part is what people seem to struggle with the least. "Verifiable" is sometimes an issue, but it's the "minimal" that folks somehow seem not to grasp. And I guess they are unwilling to follow the nice hyperlink to the explanation of what we mean. – John Bollinger Feb 09 '18 at 14:58
  • To be clear, you're not likely to get an answer until you provide something that *actually is an MCVE*... Not in this community. You'll get your question closed, instead. If you don't want to put in the effort to learn and research the best ways to debug and demonstrate problems, we simply can't help you! – autistic Feb 09 '18 at 15:00
  • I ran the code in your most recent edit, and it outputs a run time of anywhere between 0.65 and 0.7 seconds. Seems to be working fine. – dbush Feb 09 '18 at 15:03
  • Your latest latest code runs fine for me, too, producing a result of around 0.7s. – John Bollinger Feb 09 '18 at 15:20
  • @JohnBollinger Thanks for the verification. I think I might just try to fix the shm issue first and check over my thread setup again and post an update – Brain Meme Feb 09 '18 at 15:28

1 Answers1

1

In the comments people are asking you to provide an "MCVE". Since the question is about gettimeofday, we can make a much more minimal program by stripping out all the matrix multiply and shared memory stuff, and just write this:

#include <stdio.h>
#include <unistd.h>
#include <sys/time.h>

int main()
{
    struct timeval start_time, stop_time, elapsed_time;
    gettimeofday(&start_time,NULL);
    sleep(10);
    gettimeofday(&stop_time,NULL);
    timersub(&stop_time, &start_time, &elapsed_time); 
    printf("Total time was %f sec\n",
        elapsed_time.tv_sec+elapsed_time.tv_usec/1000000.0);
}

Now this is derived directly from your code; I didn't really change anything. And when I compile it on my machine, it prints

Total time was 10.004801 sec

So I conclude that your gettimeofday code is fine.

Steve Summit
  • 45,437
  • 7
  • 70
  • 103
  • Ah, so that's what's considered minimum? Thank you for the example. I think I understand now. I thought I had to include relevant functions to maintain the same program. – Brain Meme Feb 09 '18 at 15:03
  • I justify the +1 for demonstration of a proper MCVE, even though this should be a comment (not an answer), and the question should be closed (due to no problem/lack of MCVE/whatever). It should be as small as possible, able to print on one page, @BrainMeme... on top of the other criteria being met, of course... The "M" in "MCVE" is just one letter. – autistic Feb 09 '18 at 15:03
  • 1
    @BrainMeme It all depends on what's relevant *to the question you're asking*. There may well be interesting aspects of your shared memory and matrix-multiply code, but those aren't relevant to the question of whether `gettimeofday` is working, so I stripped them out. And what I demonstrated to you is exactly how I would have debugged this problem myself, if I had doubts about how `gettimeofday` worked, and if I wanted to investigate it without possibility of confusion due to interaction with irrelevant or confusing other factors. – Steve Summit Feb 09 '18 at 15:06
  • @BrainMeme, inasmuch as this does not replicate your problem, it is not a "verifiable example" for your purposes. It is, however, representative of what we're looking for in "minimal" with respect to what it *does* demonstrate. It also shows that you are probably mischaracterizing your problem, and -- I strongly suspect -- not even including the key bits in what you've presented. – John Bollinger Feb 09 '18 at 15:15
  • 1
    @Sebivor You're absolutely right: This "answer" is basically just a comment, except of course that the code fragment wasn't quite minimal enough to cram into an actual comment. – Steve Summit Feb 09 '18 at 15:26
  • I accept this as a method to convey that useful tip. You could extend the advice by suggesting that OP don't reinvent the wheel; his app could essentially be `time`d (in console environment)... or profiled using `gprof`... rather than `gettimeofday` embedded into the source code like this. With that in mind, the MCVE becomes not much more than: `int main(void) { sleep(10); }`... – autistic Feb 09 '18 at 15:46