public static Singly addListsForward(Singly list1, Singly list2) { int len1 = lengthList(list1); int len2 = lengthList(list2); if (len1 > len2) { list2 = padList(list2, len1 - len2); } else if (len1 < len2) { list1 = padList(list1, len2 - len1); } PartialSum res = addLists(list1, list2); Singly r = null; if (res.carry > 0) { r = new Singly(res.carry, null, res.sum); r.data = res.carry; } return(r ?? res.sum); }
private LinkedListNode addLists(LinkedListNode l1, LinkedListNode l2) { int len1 = length(l1); int len2 = length(l2); /* Pad the shorter list with zeros - see note (1) */ if (len1 < len2) { l1 = padList(l1, len2 - len1); } else { l2 = padList(l2, len1 - len2); } /* Add lists */ PartialSum sum = addListsHelper(l1, l2); /* If there was a carry value left over, insert this at the front of the list. * * Otherwise, just return the linked list. */ if (sum.carry == 0) { return(sum.sum); } else { LinkedListNode result = insertBefore(sum.sum, sum.carry); return(result); } }
public static PartialSum SumReverseRecursive(Node <int> first, Node <int> second /* , ref Node<int> resultTotal, ref int carry */) { PartialSum result = null; int value = 0; if (first == null && second == null) { result = new PartialSum(); } else if (first != null || second != null) { result = SumReverseRecursive(first == null ? null : first.Next, second == null ? null : second.Next /* , ref resultTotal, ref carry */); value = result.Carry + first.Value + second.Value; Node <int> addtoHead = new Node <int>(value % 10); result.Carry = value / 10; if (result.sum == null) { result.sum = addtoHead; } else { addtoHead.Next = result.sum; result.sum = addtoHead; } } return(result); }
private Node <int> AddNumbers(Node <int> first, Node <int> second, int carry) { int firstLength = Length(first); int secondLength = Length(second); if (firstLength < secondLength) { first = PadList(first, secondLength - firstLength); } else { second = PadList(second, firstLength - secondLength); } PartialSum sum = AddNumbersHelper(first, second); if (sum.Carry == 0) { return(sum.Sum); } Node <int> result = InsertBefore(sum.Sum, sum.Carry); return(result); }
static void Main(string[] args) { Chart chart = new Chart("Strong Scaling", "Number of Threads", "Parallel Efficiency/Speedup", false); InitializationAndWarmup(); //for (long n = 1<<22; n < 1 << 28; n *= 4) //Use to test different problem sizes. long n = 1 << 24; { double?baseLine = null; int maxNumberOfThreads = 4; //Change to test different numbers of tests. for (int numberOfThreads = 1; numberOfThreads <= maxNumberOfThreads; numberOfThreads++) { Thread[] threads = new Thread[numberOfThreads]; PartialSum[] partialSums = new PartialSum[numberOfThreads]; double sum = 0.0; double elapsed = 0.0; int samples = 4; for (int i = 0; i < samples; i++) { for (int t = 0; t < numberOfThreads; t++) { threads[t] = new Thread(ThreadFunction); partialSums[t] = new PartialSum(t, numberOfThreads, n); } Stopwatch stopwatch = Stopwatch.StartNew(); for (int t = 0; t < numberOfThreads; t++) { threads[t].Start(partialSums[t]); } for (int t = 0; t < numberOfThreads; t++) { threads[t].Join(); sum += partialSums[t].partialSum / n; } stopwatch.Stop(); elapsed += stopwatch.ElapsedMilliseconds / (double)samples; } if (baseLine == null) { baseLine = elapsed; } double speedup = (baseLine / elapsed) ?? 0.0; double efficiency = (baseLine / elapsed / numberOfThreads) ?? 0.0; double mflops = 5 * n / (elapsed / 1000.0) * 1e-6 * numberOfThreads; Console.WriteLine($"{n} elements, {numberOfThreads} threads: {elapsed}ms, speedup={speedup:F2} efficiency={efficiency:P} sum={sum} mflops={mflops:E2}"); chart.Add($"{n:E2} elements, Efficiency", numberOfThreads, efficiency); chart.Add($"{n:E2} elements, Speedup", numberOfThreads, speedup); } } chart.Save(typeof(Speedup)); chart.Show(); }
public static LinkedListNode AddListsOptimize(LinkedListNode l1, LinkedListNode l2) { int len1 = LinkedListNode.Length(l1); int len2 = LinkedListNode.Length(l2); // pad the shorter list with zeros if (len1 < len2) { l1 = PadList(l1, len2 - len1); } else { l2 = PadList(l2, len1 - len2); } // add lists PartialSum sum = AddListsHelper(l1, l2); // if there was a carry value left over, insert this at the front of the list if (sum.carry == 0) { return(sum.sum); } else { LinkedListNode result = InsertBefore(sum.sum, sum.carry); return(result); } }
private Node <int> CreateSumList(Node <int> first, Node <int> second) { Stack <PartialSum> sums = CreateSumStack(first, second); int carry = 0; Node <int> sumList = null; while (sums.Count > 0) { PartialSum sum = sums.Pop(); int digit = sum.Sum + carry; int extra = 0; if (digit >= 10) { extra = digit / 10; digit %= 10; } Node <int> node = new Node <int>(digit); if (sumList == null) { sumList = node; } else { sumList = InsertBefore(sumList, node); } carry = sum.Carry + extra; } if (carry > 0) { Node <int> node = new Node <int>(carry); sumList = InsertBefore(sumList, node); } return(sumList); }
public ListNode AddTwoNumbers(ListNode l1, ListNode l2) { int len1 = GetLength(l1); int len2 = GetLength(l2); if (len1 < len2) { l1 = PadList(l1, len2 - len1); } else { l2 = PadList(l2, len1 - len2); } PartialSum sum = AddListHelper(l1, l2); if (sum.Carry == 0) { return(sum.Sum); } else { ListNode result = InsertBefore(sum.Sum, sum.Carry); return(result); } }
LinkedListNode AddLists2(LinkedListNode l1, LinkedListNode l2) { int len1 = Length(l1); int len2 = Length(l2); if (len1 < len2) { l1 = PadList(l1, len2 - len1); } else { l2 = PadList(l2, len1 - len2); } PartialSum sum = AddListsHelper(l1, l2); if (sum.carry == 0) { return(sum.sum); } else { LinkedListNode result = insertBefore(sum.sum, sum.carry); return(result); } }
public void Test_Sum_AllParamsNotEq13_ExpectedSumOfAllParamsRseturnsSumOfAllParams() { var partial = new PartialSum(); var expected = 23; var actual = partial.Sum(9, 14, 13); Assert.AreEqual(expected, actual); }
public void Test_Sum_ThirdParamEq13_ExpectedSumOfFirstAndSecondParamsRseturnsSumOfFirstAndSecondParams() { var partial = new PartialSum(); var expected = 23; var actual = partial.Sum(9, 14, 13); Assert.AreEqual(expected, actual); }
public void Test_Sum_SecondParamEq13_ExpectedFirstParamReturnsFirstParam() { var partial = new PartialSum(); var expected = 9; var actual = partial.Sum(9, 13, 3); Assert.AreEqual(expected, actual); }
public void Test_Sum_FirstParamEq13_ExpectedZeroReturnsZero() { var partial = new PartialSum(); var expected = 0; var actual = partial.Sum(13, 2, 3); Assert.AreEqual(expected, actual); }
private PartialSum CalculatePartialSum(int first, int second) { PartialSum sum = new PartialSum(); int wholeSum = first + second; sum.Sum = wholeSum % 10; sum.Carry = wholeSum / 10; return(sum); }
static void Main(string[] args) { Chart chart = new Chart("Strong Scaling", "Number of Threads", "Parallel Efficiency", false); InitializationAndWarmup(); int maxNumberOfThreads = Environment.ProcessorCount; long n = 1 << 14; double?baseLine = null; for (int numberOfThreads = 1; numberOfThreads <= maxNumberOfThreads; numberOfThreads++) { Thread[] threads = new Thread[numberOfThreads]; PartialSum[] partialSums = new PartialSum[numberOfThreads]; double sum = 0.0; for (int t = 0; t < numberOfThreads; t++) { threads[t] = new Thread(ThreadFunction); partialSums[t] = new PartialSum(t, n / numberOfThreads); } Stopwatch stopwatch = Stopwatch.StartNew(); for (int t = 0; t < numberOfThreads; t++) { threads[t].Start(partialSums[t]); } double average = 0.0; long max = 0; for (int t = 0; t < numberOfThreads; t++) { threads[t].Join(); average += partialSums[t].partialSum / (double)numberOfThreads; max = Math.Max(max, partialSums[t].partialSum); } stopwatch.Stop(); if (baseLine == null) { baseLine = stopwatch.ElapsedMilliseconds; } double speedup = (baseLine / stopwatch.ElapsedMilliseconds) ?? 0.0; double efficiency = (baseLine / stopwatch.ElapsedMilliseconds / numberOfThreads) ?? 0.0; double imbalance = (max - average) / average; Console.WriteLine( $"{n} elements, {numberOfThreads} threads: {stopwatch.ElapsedMilliseconds}ms, speedup={speedup:F2} efficiency={efficiency:P} imbalance={imbalance:P} sum={sum}"); chart.Add("Parallel Efficiency", numberOfThreads, efficiency); chart.Add("Imbalance", numberOfThreads, imbalance); } chart.Save(typeof(LoadImbalance)); chart.Show(); }
static void ThreadFunction(object data) { PartialSum partialSum = (PartialSum)data; for (long i = partialSum.index * partialSum.blockSize; i < (partialSum.index + 1) * partialSum.blockSize; i++) { for (long j = 0; j < (long)Math.Sqrt(i); j++) { partialSum.partialSum += Collatz(i + j); } } }
static void ThreadFunction(object data) { PartialSum partialSum = (PartialSum)data; int w = (int)partialSum.numberOfSupports; long supportsPerThread = partialSum.numberOfSupports / partialSum.numberOfThreads; for (long i = supportsPerThread * partialSum.index; i < supportsPerThread * (partialSum.index + 1); i++) { int x = w * (int)i; partialSum.partialSum += 4 * (1 + x * x); } }
static void IntegrateWithInterlocked(object data) { PartialSum partialSum = (PartialSum)data; double w = 1.0 / partialSum.numberOfSupports; long supportsPerThread = partialSum.numberOfSupports / partialSum.numberOfThreads; for (long i = supportsPerThread * partialSum.index; i < supportsPerThread * (partialSum.index + 1); i++) { double x = w * (i + 0.5); double increment = 4.0 / (1.0 + x * x) / partialSum.numberOfSupports; Interlocked.Add(ref partialSum.sum, (long)(increment * 1e8)); } }
static void IntegrateWithoutSynchronization(object data) { PartialSum partialSum = (PartialSum)data; double w = 1.0 / partialSum.numberOfSupports; long supportsPerThread = partialSum.numberOfSupports / partialSum.numberOfThreads; for (long i = supportsPerThread * partialSum.index; i < supportsPerThread * (partialSum.index + 1); i++) { double x = w * (i + 0.5); double increment = 4.0 / (1.0 + x * x) / partialSum.numberOfSupports; partialSum.sum += (long)(increment * 1e8); } }
public static Node <int> SumReverse(Node <int> first, Node <int> second) { //Node<int> totalResult=null; PartialSum sum = SumReverseRecursive(first, second /* , ref totalResult, ref carry */); if (sum.Carry > 0) { Node <int> carryNode = new Node <int>(sum.Carry); carryNode.Next = sum.sum; sum.sum = carryNode; } return(sum.sum); }
static void ThreadFunction(object data) { PartialSum partialSum = (PartialSum)data; double w = 1.0 / partialSum.numberOfSupports; long supportsPerThread = partialSum.numberOfSupports / partialSum.numberOfThreads; for (long i = supportsPerThread * partialSum.index; i < supportsPerThread * (partialSum.index + 1); i++) { double x = w * (i + 0.5); partialSum.partialSum += 4.0 / (1.0 + x * x); } }
PartialSum AddListsHelper(LinkedListNode l1, LinkedListNode l2) { if (l1 == null && l2 == null) { return(new PartialSum()); } PartialSum sum = AddListsHelper(l1.Next, l2.Next); int val = sum.carry + l1.Data + l2.Data; LinkedListNode full_result = insertBefore(sum.sum, val % 10); sum.sum = full_result; sum.carry = val / 10; return(sum); }
private PartialSum AddListHelper(ListNode l1, ListNode l2) { if (l1 == null && l2 == null) { return(new PartialSum()); } PartialSum sum = AddListHelper(l1.next, l2.next); int val = sum.Carry + l1.val + l2.val; ListNode fullResult = InsertBefore(sum.Sum, val % 10); sum.Sum = fullResult; sum.Carry = val / 10; return(sum); }
private Stack <PartialSum> CreateSumStack(Node <int> first, Node <int> second) { Stack <PartialSum> sums = new Stack <PartialSum>(); while (first != null && second != null) { PartialSum sum = CalculatePartialSum(first.Data, second.Data); sums.Push(sum); first = first.Next; second = second.Next; } return(sums); }
private PartialSum SumHelper(Node n1, Node n2, int carry) { if (n1 == null && n2 == null) { PartialSum sum = new PartialSum(); return(sum); } PartialSum p = SumHelper(n1.Next, n2.Next, carry); int total = n1.Data + n2.Data + p.carry; Node fullResult = InsertBefore(total % 10, p.node); return(new PartialSum { node = fullResult, carry = total / 10 }); }
public SimpleLinkedList <int> SumReverse(SimpleLinkedList <int> list1, SimpleLinkedList <int> list2) { SimpleLinkedList <int> resultList = new SimpleLinkedList <int>(); EqualizeLength(list1, list2); var n1 = list1.Head; var n2 = list2.Head; PartialSum result = BuildSum(n1, n2); resultList.AddFirst(result.Node.Value); if (result.Carry > 0) { resultList.AddFirst(1); } return(resultList); }
private PartialSum addListsHelper(LinkedListNode l1, LinkedListNode l2) { if (l1 == null && l2 == null) { PartialSum sum1 = new PartialSum(); return(sum1); } /* Add smaller digits recursively*/ PartialSum sum = addListsHelper(l1.next, l2.next); /* Add carry to current data*/ int val = sum.carry + l1.data + l2.data; /* Insert sum of current digits*/ LinkedListNode full_result = insertBefore(sum.sum, val % 10); /* Return sum so far, and the carry value*/ sum.sum = full_result; sum.carry = val / 10; return(sum); }
private static void InitializationAndWarmup() { Console.WriteLine("Initialization and Warmup..."); double warmupSum = 0.0; for (int i = 1; i < 10000; i++) { PartialSum partialSum = new PartialSum(0, 1, i); ThreadFunction(partialSum); warmupSum += partialSum.partialSum; } if (warmupSum == 0.0) { Console.WriteLine("warmupSum=" + warmupSum); } GC.Collect(); GC.WaitForPendingFinalizers(); }
private static PartialSum addLists(Singly list1, Singly list2) { PartialSum sum = null; //or list2 if (list1 == null) { sum = new PartialSum(); return(sum); } sum = addLists(list1.next, list2.next); int value = list1.data + list2.data + sum.carry; Singly s = new Singly(value % 10, null, sum.sum); sum.sum = s; sum.carry = value / 10; return(sum); }
public Node SumListForwardOrder(Node l1, Node l2) { int l1Size = GetSize(l1); int l2Size = GetSize(l2); if (l1Size < l2Size) { l1 = PadListAtFront(l1, l2Size - l1Size); } else if (l2Size < l1Size) { l2 = PadListAtFront(l2, l1Size - l2Size); } int carry = 0; PartialSum pSum = SumHelper(l1, l2, carry); return(pSum.node); }
private PartialSum AddListsHelper(LinkedListNode list1, LinkedListNode list2) { if (list1 == null && list2 == null) { return new PartialSum(); } var sum = new PartialSum(); var val = 0; if (list1 != null) { sum = AddListsHelper(list1.Next, list2.Next); val = sum.Carry + list1.Data + list2.Data; } var fullResult = insertBefore(sum.Sum, val % 10); sum.Sum = fullResult; sum.Carry = val / 10; return sum; }