zx2*_*228 5 distributed bigint parallelism-amdahl chapel
我正在使用非常大的bigint数字,我需要将它们写入磁盘并稍后再读取它们,因为它们一次都不适合内存.
当前的Chapel实现首先将其转换bigint为a string,然后将其写入string磁盘[1].这对于大整数来说需要很长时间.
var outputFile = open("outputPath", iomode.cwr);
var writer = outputFile.writer();
writer.write(reallyLargeBigint);
writer.close();
outputFile.close();
Run Code Online (Sandbox Code Playgroud)
有没有办法使用GMP的mpz_out_raw()/ mpz_inp_raw()[2]或mpz_export()/ mpz_import()[3]或其他类似的方式bigint直接将字节转储到磁盘而不事先转换为字符串然后将字节读回到bigint对象?
这也适用于bigint阵列吗?
如果在当前状态下不可能将这些功能添加到Chapel的标准库中,怎么可能?
[1] https://github.com/chapel-lang/chapel/blob/master/modules/standard/BigInteger.chpl#L346
[2] https://gmplib.org/manual/I_002fO-of-Integers.html
[3] https://gmplib.org/manual/Integer-Import-and-Export.html
您提到的功能在任何 Chapel 模块中都不能直接使用,但您可以编写extern过程和extern类型来访问GMP函数。
首先,我们需要能够使用 C 文件,因此为它们声明一些过程和类型:
extern type FILE;
extern type FILEptr = c_ptr(FILE);
extern proc fopen(filename: c_string, mode: c_string): FILEptr;
extern proc fclose(fp: FILEptr);
Run Code Online (Sandbox Code Playgroud)
然后我们就可以声明我们需要的GMP函数了:
extern proc mpz_out_raw(stream: FILEptr, const op: mpz_t): size_t;
extern proc mpz_inp_raw(ref rop: mpz_t, stream: FILEptr): size_t;
Run Code Online (Sandbox Code Playgroud)
现在我们可以使用它们来写入一个bigint值:
use BigInteger;
var res: bigint;
res.fac(100); // Compute 100!
writeln("Writing the number: ", res);
var f = fopen("gmp_outfile", "w");
mpz_out_raw(f, res.mpz);
fclose(f);
Run Code Online (Sandbox Code Playgroud)
并从文件中读回它:
var readIt: bigint;
f = fopen("gmp_outfile", "r");
mpz_inp_raw(readIt.mpz, f);
fclose(f);
writeln("Read the number:", readIt);
Run Code Online (Sandbox Code Playgroud)
对于值数组,bigint只需循环它们即可写入或读取它们:
// initialize the array
var A: [1..10] bigint;
for i in 1..10 do
A[i].fac(i);
// write the array to a file
f = fopen("gmp_outfile", "w");
for i in 1..10 do
mpz_out_raw(f, A[i].mpz);
fclose(f);
// read the array back in from the file
var B: [1..10] bigint;
f = fopen("gmp_outfile", "r");
for i in 1..10 do
mpz_inp_raw(B[i].mpz, f);
fclose(f);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
200 次 |
| 最近记录: |