Home > Software engineering >  Two large array multiplication, have some fast algorithm? Thank you for guidance
Two large array multiplication, have some fast algorithm? Thank you for guidance

Time:09-15

I have two arrays A and B, for example, each array includes 10 million different number, the data type for the double
I want to do is A1 * B1, B2, A2 * and, calculate 10 million

What is the calculation algorithm faster?

Thank you very much!

CodePudding user response:

Can consider the data segment multi-threaded operation, each thread operation for a period of

CodePudding user response:

Parallel computing,,,,,,,,,,,,,,,,,,,,,,

CodePudding user response:

Can consider the data segment multi-threaded operation, each thread operation, good trouble

CodePudding user response:

In addition to the multithreading, also can consider SIMD, support AVX instructions can be used, the following is a multithreaded
 # include & lt; Windows. H> 
#include
Typedef struct _MyParameter
{
Double * A * B * C.
Int start, length;
} MyParameter, * PMyParameter;

DWORD multiply_thread (PVOID pv)
{
PMyParameter p=(PMyParameter) pv;
For (int I=p - & gt; The start; i

{
P - & gt; [I]=p - C & gt; A [I] * p - & gt; B [I];
}
return 0;
}

Void multiply (A, double * * B double, double * C, int length)
{
SYSTEM_INFO SYSTEM_INFO;
GetSystemInfo (& amp; System_info);
//the number of threads for logical CPU number 2 x
Int numberOfThreads=system_info. DwNumberOfProcessors * 2;
//thread handle array
PHANDLE handles=(PHANDLE) malloc (sizeof (HANDLE) * numberOfThreads);
If (handles==NULL)
{
return;
}
//parameter array
PMyParameter parameters=(PMyParameter) malloc (sizeof (MyParameter) * numberOfThreads);
If the parameters (==NULL)
{
return;
}
//each thread length calculation
Int lengthPerThread=length/numberOfThreads;
MyParameter parameter={A, B, C, 0, lengthPerThread};
//multi-threaded calculation
for (int i=0; i {
The parameters [I]=parameter;
The parameters [I]. Start=I * lengthPerThread;
Handles [I]=CreateThread (NULL, 0, multiply_thread, the parameters + I, NULL, NULL);
}
For (int I=length/numberOfThreads * numberOfThreads; i {
C=[I] [I] A [I] * B;
}
WaitForMultipleObjects (numberOfThreads, handles, TRUE, INFINITE).
Free (handles);
Free (parameters);
}

CodePudding user response:

For (int I=length/numberOfThreads * numberOfThreads; i {
C=[I] [I] A [I] * B;
}


Length/numberOfThreads * numberOfThreads
Here is a slip of the pen?

CodePudding user response:

Parallel computing is the best way,

CodePudding user response:

Block, block number is the number of CPU core 1 ~ 2 times, so efficiency is higher
  • Related