bool miller_rabin(LL n,int s=100) { // iterate s times of witness on n // return 1 if prime, 0 otherwise if(n<2) return 0; if(!(n&1)) return n == 2; LL u=n-1; int t=0; // n-1 = u*2^t while(!(u&1)) u>>=1, t++; while(s--){ LL a=randll()%(n-1)+1; if(witness(a,n,u,t)) return 0; } return 1; }
/*------------------------------------------------------------------------- * Function: writer * * Purpose: Creates a *big* dataset. * * Return: Success: 0 * * Failure: >0 * * Programmer: Robb Matzke * Wednesday, April 8, 1998 * * Modifications: * Robb Matzke, 15 Jul 1998 * Addresses are written to the file DNAME instead of stdout. * *------------------------------------------------------------------------- */ static int writer (char* filename, hid_t fapl, fsizes_t testsize, int wrt_n) { hsize_t size1[4] = {8, 1024, 1024, 1024}; hsize_t size2[1] = {GB8LL}; hsize_t hs_start[1]; hsize_t hs_size[1]; hid_t file=-1, space1=-1, space2=-1, mem_space=-1, d1=-1, d2=-1; int *buf = (int*)HDmalloc (sizeof(int) * WRT_SIZE); int i, j; FILE *out = HDfopen(DNAME, "w"); hid_t dcpl; switch(testsize){ case LFILE: TESTING("Large dataset write(2GB)"); /* reduce size1 to produce a 2GB dataset */ size1[1] = 1024/16; size2[0] /= 16; break; case XLFILE: TESTING("Extra large dataset write(4GB)"); /* reduce size1 to produce a 4GB dataset */ size1[1] = 1024/8; size2[0] /= 8; break; case HUGEFILE: TESTING("Huge dataset write"); /* Leave size1 as 32GB */ break; case SFILE: TESTING("small dataset write(1GB)"); /* reduce size1 to produce a 1GB dataset */ size1[1] = 1024/32; size2[0] /= 32; break; case NOFILE: /* what to do?? */ HDfprintf(stdout, "Unexpected file size of NOFILE\n"); goto error; break; default: HDfprintf(stdout, "Unexpected file size(%d)\n", testsize); goto error; break; } /* * We might be on a machine that has 32-bit files, so create an HDF5 file * which is a family of files. Each member of the family will be 1GB */ if ((file=H5Fcreate(filename, H5F_ACC_TRUNC, H5P_DEFAULT, fapl)) < 0) { goto error; } /* Create simple data spaces according to the size specified above. */ if ((space1 = H5Screate_simple (4, size1, size1)) < 0 || (space2 = H5Screate_simple (1, size2, size2)) < 0) { goto error; } /* Create the datasets */ /* * The fix below is provided for bug#921 * H5Dcreate with H5P_DEFAULT creation properties * will create a set of solid 1GB files; test will crash if quotas are enforced * or it will take some time to write a file. * We should create a dataset allocating space late and never writing fill values. * EIP 4/8/03 */ dcpl = H5Pcreate(H5P_DATASET_CREATE); H5Pset_alloc_time(dcpl, H5D_ALLOC_TIME_LATE); H5Pset_fill_time(dcpl, H5D_FILL_TIME_NEVER); if((d1 = H5Dcreate2(file, "d1", H5T_NATIVE_INT, space1, H5P_DEFAULT, dcpl, H5P_DEFAULT)) < 0 || (d2 = H5Dcreate2(file, "d2", H5T_NATIVE_INT, space2, H5P_DEFAULT, dcpl, H5P_DEFAULT)) < 0) { goto error; } /* Write some things to them randomly */ hs_size[0] = WRT_SIZE; if ((mem_space = H5Screate_simple (1, hs_size, hs_size)) < 0) goto error; for (i=0; i<wrt_n; i++) { /* start position must be at least hs_size from the end */ hs_start[0] = randll (size2[0]-hs_size[0], i); HDfprintf (out, "#%03d 0x%016Hx\n", i, hs_start[0]); if (H5Sselect_hyperslab (space2, H5S_SELECT_SET, hs_start, NULL, hs_size, NULL) < 0) goto error; for (j=0; j<WRT_SIZE; j++) { buf[j] = i+1; } if (H5Dwrite (d2, H5T_NATIVE_INT, mem_space, space2, H5P_DEFAULT, buf) < 0) goto error; } if (H5Dclose (d1) < 0) goto error; if (H5Dclose (d2) < 0) goto error; if (H5Sclose (mem_space) < 0) goto error; if (H5Sclose (space1) < 0) goto error; if (H5Sclose (space2) < 0) goto error; if (H5Fclose (file) < 0) goto error; HDfree (buf); HDfclose(out); PASSED(); return 0; error: H5E_BEGIN_TRY { H5Dclose(d1); H5Dclose(d2); H5Sclose(space1); H5Sclose(space2); H5Sclose(mem_space); H5Fclose(file); } H5E_END_TRY; if (buf) HDfree(buf); if (out) HDfclose(out); return 1; }
/*------------------------------------------------------------------------- * Function: writer * * Purpose: Creates a *big* dataset. * * Return: Success: 0 * * Failure: >0 * * Programmer: Robb Matzke * Wednesday, April 8, 1998 * * Modifications: * Robb Matzke, 15 Jul 1998 * Addresses are written to the file DNAME instead of stdout. * *------------------------------------------------------------------------- */ static int writer(char* filename, hid_t fapl, int wrt_n) { hsize_t size1[4] = {8, 1024, 1024, 1024}; hsize_t size2[1] = {GB8LL}; hsize_t hs_start[1]; hsize_t hs_size[1]; hid_t file=-1, space1=-1, space2=-1, mem_space=-1, d1=-1, d2=-1; int *buf = (int*)malloc (sizeof(int) * WRT_SIZE); int i, j; FILE *out = fopen(DNAME, "w"); hid_t dcpl; TESTING("large dataset write"); /* * We might be on a machine that has 32-bit files, so create an HDF5 file * which is a family of files. Each member of the family will be 1GB */ if ((file=H5Fcreate(filename, H5F_ACC_TRUNC, H5P_DEFAULT, fapl))<0) { goto error; } /* Create simple data spaces according to the size specified above. */ if ((space1 = H5Screate_simple (4, size1, size1))<0 || (space2 = H5Screate_simple (1, size2, size2))<0) { goto error; } /* Create the datasets */ /* * The fix below is provided for bug#921 * H5Dcreate with H5P_DEFAULT creation properties * will create a set of solid 1GB files; test will crash if quotas are enforced * or it will take some time to write a file. * We should create a dataset allocating space late and never writing fill values. * EIP 4/8/03 if ((d1=H5Dcreate (file, "d1", H5T_NATIVE_INT, space1, H5P_DEFAULT))<0 || (d2=H5Dcreate (file, "d2", H5T_NATIVE_INT, space2, H5P_DEFAULT))<0) { goto error; } */ dcpl = H5Pcreate(H5P_DATASET_CREATE); H5Pset_alloc_time(dcpl, H5D_ALLOC_TIME_LATE); H5Pset_fill_time(dcpl, H5D_FILL_TIME_NEVER); if ((d1=H5Dcreate (file, "d1", H5T_NATIVE_INT, space1, dcpl))<0 || (d2=H5Dcreate (file, "d2", H5T_NATIVE_INT, space2, dcpl))<0) { goto error; } /* Write some things to them randomly */ hs_size[0] = WRT_SIZE; if ((mem_space = H5Screate_simple (1, hs_size, hs_size))<0) goto error; for (i=0; i<wrt_n; i++) { hs_start[0] = randll (size2[0], i); HDfprintf (out, "#%03d 0x%016Hx\n", i, hs_start[0]); if (H5Sselect_hyperslab (space2, H5S_SELECT_SET, hs_start, NULL, hs_size, NULL)<0) goto error; for (j=0; j<WRT_SIZE; j++) { buf[j] = i+1; } if (H5Dwrite (d2, H5T_NATIVE_INT, mem_space, space2, H5P_DEFAULT, buf)<0) goto error; } if (H5Dclose (d1)<0) goto error; if (H5Dclose (d2)<0) goto error; if (H5Sclose (mem_space)<0) goto error; if (H5Sclose (space1)<0) goto error; if (H5Sclose (space2)<0) goto error; if (H5Fclose (file)<0) goto error; free (buf); fclose(out); PASSED(); return 0; error: H5E_BEGIN_TRY { H5Dclose(d1); H5Dclose(d2); H5Sclose(space1); H5Sclose(space2); H5Sclose(mem_space); H5Fclose(file); } H5E_END_TRY; if (buf) free(buf); if (out) fclose(out); return 1; }