c++ - Multithread Programming for memcpy -
I'm doing an optimization function for the memcpy function, I found this link here.
Since I am not familiar with multithread programming, I do not know how to put the code into the original main function? How to modify the code in the original question in the Multithrand Memkpi project? I mean, how to prepare a whole project for this multithread memcpy project. Where there are places to insert tasks such as startCopyThreads or stopCopyThreads or mt_memcpy functions in the original main work
#define NUM_CPY_THREADS 4 handle hCopyThreads [NUM_CPY_THREADS] = {0}; Handle hCopyStartSemaphores [NUM_CPY_THREADS] = {0}; Handle hCopyStopSemaphores [NUM_CPY_THREADS] = {0}; Typefif Structure {ET CT; Zero * src, * dest; Size_t size; } Mt_cpy_t; Mt_cpy_t mtParameters [NUM_CPY_THREADS] = {0}; DWORD WINAPI thread_copy_proc (LPVOD Ultimate) {mt_cpy_t * p = (mt_cpy_t *) Ultimate; While (1) {WaitForSingleObject (hCopyStartSemaphores [p-> gt; ct], INFINITE); Mempi (p-> gt; destruction, p-> source, p- & gt; shape); Release Sander (HPP staphophores (P-> CT), 1, faucet); } Return 0; } StartCopyThreads () {int (integer CTR = 0; CTR & lt; NUM_CPY_THREADS; CTR ++)} {hCopyStartSemaphores [CTR] = CreateSemaphore (zero, 0, 1, zero); HCopyStopSemaphores [ctr] = CreateSemaphore (NULL, 0, 1, NULL); MtParameters [CTR]. CT = CTR; HCopyThreads [CTR] = CreateThread (0, 0, thread_copy_proc, & amp; mtParameters [CTR], 0, zero); } Return 0; } Zero * mt_memcpy (Zero * Destination, Zero * src, size_t bytes) {// set for the above parameters (integer CTR = 0; CTR & lt; NUM_CPY_THREADS; CTR ++) {mtParameters [CTR] .dest = ( Char *) Destination + CTR * bytes / NUM_CPY_THREADS; MtParamter [ctr] .src = (four *) src + ctr * bytes / NUM_CPY_THREADS; MtParameters [CTR] Size = (CTR + 1) * bytes / NUM_CPY_THREADS - CTR * bytes / NUM_CPY_THREADS; } // to begin calculation for release semaphore (int CTR = 0; CTR's; NUM_CPY_THREADS; CTR ++) ReleaseSemaphore (hCopyStartSemaphores [CTR], 1, zero); // Wait for all threads to wait WaitForMultipleObjects (NUM_CPY_THREADS, hCopyStopSemaphores, TRUE, INFINITE); in return; } StopCopyThreads () {int (integer CTR = 0; CTR & lt; NUM_CPY_THREADS; CTR ++)} {TerminateThread (hCopyThreads [CTR], 0); CloseHandle (hCopyStartSemaphores [CTR]); CloseHandle (hCopyStopSemaphores [CTR]); } Return 0; }
It relies heavily on architecture and OS.
With a processor:
If you are using thread on machine with only 1 core for memcpy , then this speed will not show up. Reason, for all threads running on a processor, context switching will occur which will be an overhead when using the mailcky without the use of threads. With multicore:
In this case, it also depends on the kernel, even if it does not map your threads to different processors, as these threads User level will not be there if your threads are on different processors running simultaneously, you can see speedup if the dual port has access to the archive. With a single port reach, I'm not sure whether it will improve or not.
Comments
Post a Comment