-2

I have the following code for matrix-vector multiplication:

std::chrono::steady_clock::time_point start, end2;
void fillMatrixConditions(LPVOID lpv) {
    end2 = std::chrono::steady_clock::now();
    std::cout << "Time difference = " << std::chrono::duration_cast<std::chrono::microseconds>(end2 - start).count() << std::endl;
    std::cout << "Time difference = " << std::chrono::duration_cast<std::chrono::nanoseconds> (end2 - start).count() << std::endl;
    int i, j = horizontControl;
    for (i = 0; i < horizontControl; i++, j++) {
        b[i] = akcni - uMin;
        b[j] = uMax - akcni;
    }
    j = 2 * horizontControl + N - N1 + 1;
    int k = 0;
    for (i = 2 * i; i < (2 * horizontControl + N - N1 + 1); i++, j++, k++) {
        b[i] = MpDup[k] - yMin;
        b[j] = yMax - MpDup[k];
    };
    RtPrintf("Thread");
}

int _tmain(int argc, _TCHAR * argv[])
{
    HANDLE hThread;
    DWORD id;
    int k = 0;
    double temp;
    double g[50];       
    for (int j = 0; j < N - N1 + 1; j++)
    {
        temp = 0;
        for (int m = 0; m < horizontPrediction - 1; m++, k++)
        {
            temp = temp + Mp[k] * dup[m];
        }
        MpDup[j] = temp;
        tempMatrix[j] = setPoint - y - temp;
    }
    start = std::chrono::steady_clock::now();
    hThread = CreateThread(NULL, NULL, (LPTHREAD_START_ROUTINE)fillMatrixConditions, NULL, NULL, &id);      
    k = 0;
    for (int j = 0; j < horizontControl; j++)
    {
        temp = 0;
        for (int m = 0; m < N - N1 + 1; m++, k++)
        {
            temp = temp + Mtranspose[k] * tempMatrix[m];
        }
        g[j] = -2 * temp;
    }
    RtPrintf("Point\n");    
    WaitForSingleObject(hThread, INFINITE);
    CloseHandle(hThread);
    return 0;
}

But CreateThread() is too slow, because this is the output of the program:

Point
Time difference = 247
Time difference = 247261

I thought the first thing to be written was going to be the Time difference in thread and then "Point". Or is this output normal? I must use CreateThread(). I can't pthread etc. Matrix Mtranspose has 2025 cells.

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
Filip Procházka
  • 187
  • 4
  • 13
  • Make some effort to create a Minimal, Complete, and Verifiable example. It wouldn't be hard in this case: http://stackoverflow.com/help/mcve – zeromus May 07 '16 at 14:39
  • Yes, creating threads, in general, is quite expensive. – Stefan May 07 '16 at 14:39
  • Your timing is not accurate. You are timming two things. 1) how long it takes to create the thread and 2) how long the main thread takes to execute a doubly nested loop. Just becuase you create a thread does not mean it starts executing immediately. It must wait until there is a core available to execute on. In the above code it is waiting for the main thread to relinquish the core before it even starts executing. – Martin York May 07 '16 at 14:56
  • zeromus - Sorry for my ignorance. I will try to write better next time. Otherwise I thanks for reply. I don't have any a experience with multithreading and i thought, this can me help accelerate my code. – Filip Procházka May 07 '16 at 15:13

1 Answers1

4

This is just a really complicated way of asking:

Is it normal for CreateThread to take 247 microseconds to start executing my code?

And the answer is yes, probably it is normal. Here's a question and answer on this topic which says it took 0.015625 milliseconds to create a thread, which is 15 microseconds: How long does thread creation and termination take under Windows? Faster than your time, but not instantaneous.

If you need threads to start faster, you should use a thread pool and start the threads ahead of time.

Community
  • 1
  • 1
John Zwinck
  • 239,568
  • 38
  • 324
  • 436