简体   繁体   English

替代使用mpfr数组

[英]Alternative to using mpfr arrays

I am trying to write a function in C++ using MPFR to calculate multiple values. 我正在尝试使用MPFR在C ++中编写一个函数来计算多个值。 I am currently using an mpfr array to store those values. 我目前正在使用mpfr数组来存储这些值。 It is unknown how many values need to be calculated and stored each time. 未知每次需要计算和存储多少个值。 Here is the function: 这是函数:

void Calculator(mpfr_t x, int v, mpfr_t *Values, int numOfTerms, int mpfr_bits) {
    for (int i = 0; i < numOfTerms; i++) {
        mpfr_init2(Values[i], mpfr_bits);
        mpfr_set(Values[i], x, GMP_RNDN);
        mpfr_div_si(Values[i], Values[i], pow(-1,i+1)*(i+1)*pow(v,i+1), GMP_RNDN);
    }
}

The program itself has a while loop that has a nested for loop that takes these values and does calculations with them. 程序本身具有一个while循环,该循环具有嵌套的for循环,该循环获取这些值并对其进行计算。 In this way, I don't have to recalculate these values each time within the for loop. 这样,我不必在for循环中每次都重新计算这些值。 When the for loop is finished, I clear the memory with for循环完成后,我用

delete[] Values;

before the the while loops starts again in which case, it redeclares the array with 在while循环再次开始之前,在这种情况下,它使用

mpfr_t *Values;
Values = new mpfr_t[numOfTerms];

The number of values that need to be stored are calculated by a different function and is told to the function through the variable numOfTerms. 需要存储的值的数量由另一个函数计算,并通过变量numOfTerms告知函数。 The problem is that for some reason, the array slows down the program tremendously. 问题在于,由于某种原因,阵列会极大地减慢程序速度。 I am working with very large numbers so the thought is that if I recalculate those values each time, it gets extremely expensive but this method is significantly slower than just recalculating the values in each iteration of the for loop. 我正在处理非常大的数字,因此认为如果每次都重新计算这些值,它将变得非常昂贵,但是此方法比仅在for循环的每次迭代中重新计算值的速度要慢得多。 Is there an alternative method to this? 有替代方法吗?

EDIT** Instead of redeclaring the array over each time, I moved the declaration and the delete[] Values outside of the while loop. 编辑**而不是每次都重新声明数组,而是将声明和delete []值移到while循环之外。 Now I am just clearing each element of the array with 现在,我只是使用以下命令清除数组的每个元素

for (int i = 0; i < numOfTerms; i++) {
            mpfr_clear(Values[i]);
}

inside of the while loop before the while loop starts over. 在while循环开始之前的while循环内部。 The program has gotten noticeably faster but is still much slower than just calculating each value over. 该程序已经明显加快了速度,但比仅仅计算每个值还慢得多。

If I understand correctly, you are doing inside a while loop: mpfr_init2 (at the beginning of the iteration) and mpfr_clear (at the end of the iteration) on numOfTerms MPFR numbers, and the value of numOfTerms depends on the iteration. 如果我理解正确,则您在while循环内进行操作: mpfr_init2 (在迭代的开始)和mpfr_clear (在迭代的结束)基于numOfTerms MPFR编号,并且numOfTerms的值取决于迭代。 And this is what takes most of the time. 这就是大多数时间。

To avoid these many memory allocations by mpfr_init2 and deallocations by mpfr_clear , I suggest that you declare the array outside the while loop and initially call the mpfr_init2 outside the while loop. 为了避免mpfr_init2这么多内存分配,而mpfr_init2避免释放mpfr_clear ,我建议您在while循环外声明该数组while并首先在while循环外调用mpfr_init2 The length of the array (ie the number of terms) should be what you think is the maximum number of terms. 数组的长度(即项数)应为您认为的最大项数。 What can happen is that for some iterations, the chosen number of terms was too small. 可能发生的情况是,对于某些迭代,选择的项数太少。 In such a case, you need to increase the length of the array (this will need a reallocation) and call mpfr_init2 on the new elements. 在这种情况下,您需要增加数组的长度(这将需要重新分配),并在新元素上调用mpfr_init2 This will be the new length of the array for the remaining iterations, until the array needs to be enlarged again. 这将是剩余迭代的新数组长度,直到需要再次扩大数组为止。 After the while loop, do the mpfr_clear 's. while循环之后,执行mpfr_clear

When you need to enlarge the array, have a good strategy to choose the new number of elements. 当需要扩大数组时,有一个很好的策略来选择新的元素数量。 Just taking the needed value of numOfTerms for the current iteration may not be a good one, since it may yield many reallocations. 仅取当前迭代所需的numOfTerms值可能不是一个好方法,因为它可能会产生许多重新分配。 For instance, make sure that you have at least a N% increase. 例如,请确保您至少增加了N%。 Do some tests to choose the best value for N... See Dynamic array for instance. 做一些测试以选择N的最佳值。例如,请参阅动态数组 In particular, you may want to use the C++ implementation of dynamic arrays, as mentioned on this Wikipedia article. 特别是,您可能要使用Wikipedia文章上提到的动态数组的C ++实现。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM