I am building a program that deals with numbers in different bases, and I wanted to optimize it by using parallel programming, but I am new to all of it.
Right now, I am trying to implement a parallel Karatsuba multiplication algorithm:
public static NX MulAK(NX A, NX B){
// ¶ Safeguard:
if(A.Base != B.Base){
Console.Error.WriteLine("\tError:\nA multiplication of numbers with different bases was attempted!");
return null!;
}
// ¶ Init:
MatchLength(ref A, ref B);
// Base Case:
if(A.Len() == 1){return SingleMul(A, B[0]);}
// ¶ Init:
(NX A_L, NX A_H) = SplitHalf(A);
(NX B_L, NX B_H) = SplitHalf(B);
// ¶ Recursive calls in parallel:
NX L;
NX M;
NX H;
Parallel.Invoke(
() => {L = MulAK(A_L, B_L);},
() => {M = MulAK(A_L + B_H, A_H + B_L);},
() => {H = MulAK(A_H, B_H);}
);
// Return:
return
(L
+ (M - L - H).ShiftPow(A.Len() / 2)
+ H.ShiftPow(A.Len())
).ShiftPow(A.Powr + B.Powr);
}
It doesn't show any errors when calling the Parallel.Invoke(), but on the return it says:
Use of unassigned local variable 'L' [NumberX] csharp(CS0165)
...for L and all the others.
How can I make the recursive calls of the Karatsuba parallel to work, in my case?