void authKeyIdCreate(void *arg) { CE_AuthorityKeyID *akid = (CE_AuthorityKeyID *)arg; /* all three fields optional */ akid->keyIdentifierPresent = randBool(); if(akid->keyIdentifierPresent) { randData(&akid->keyIdentifier, 16); } akid->generalNamesPresent = randBool(); if(akid->generalNamesPresent) { akid->generalNames = (CE_GeneralNames *)malloc(sizeof(CE_GeneralNames)); memset(akid->generalNames, 0, sizeof(CE_GeneralNames)); genNamesCreate(akid->generalNames); } if(!akid->keyIdentifierPresent & !akid->generalNamesPresent) { /* force at least one to be present */ akid->serialNumberPresent = CSSM_TRUE; } else { akid->serialNumberPresent = randBool(); } if(akid->serialNumberPresent) { randData(&akid->serialNumber, 16); } }
SecureBinaryData SecureBinaryData::GenerateRandom(uint32_t numBytes) { static CryptoPP::AutoSeededRandomPool prng; SecureBinaryData randData(numBytes); prng.GenerateBlock(randData.getPtr(), numBytes); return randData; }
// compressor must be initialized! void testCompressDecompress(int size_kb, ICompressor &compressor, IDecompressor& decompressor) { GrowBuf data; GrowBuf compressed; GrowBuf decompressed; randData(data, size_kb); compress(compressor, data, compressed); decompress(decompressor, compressed, decompressed); CPPUNIT_ASSERT_MESSAGE( "decompressed data is smaller", data.getlen() <= decompressed.getlen() ); CPPUNIT_ASSERT_MESSAGE( "decompressed data is larger", data.getlen() >= decompressed.getlen() ); CPPUNIT_ASSERT_EQUAL_MESSAGE( "decompressed data is different", 0, memcmp(data.get(), decompressed.get(), data.getlen()) ); }
SecureBinaryData SecureBinaryData::GenerateRandom(uint32_t numBytes, SecureBinaryData entropy) { BTC_PRNG prng; // Entropy here refers to *EXTRA* entropy. Crypto++ has it's own mechanism // for generating entropy which is sufficient, but it doesn't hurt to add // more if you have it. if(entropy.getSize() > 0) prng.IncorporateEntropy( (byte*)entropy.getPtr(), entropy.getSize()); SecureBinaryData randData(numBytes); prng.GenerateBlock(randData.getPtr(), numBytes); return randData; }
float sizePrint(int bound) { //printf("The PhoneBook size Before : %lu\n",sizeof(PhoneOrigin)); //printf("The PhoneBook size After : %lu\n",sizeof(PhoneBook)); init(); int x,y; InsertData("John"); for(x = 0;x<bound;x++) { InsertData(randData()); } //Show(); float startTime = 0 , endTime = 0; startTime = (float)clock()/CLOCKS_PER_SEC; find(); endTime = (float)clock()/CLOCKS_PER_SEC; //printf("The split struct time \n%f\n",endTime-startTime); empty(); return endTime-startTime; }
void skidCreate(void *arg) { CSSM_DATA_PTR skid = (CSSM_DATA_PTR)arg; randData(skid, 16); }
TEST_F(TairClientRDBTest, test_hdel_expire_interval) { //set expire with interval char* skey = NULL; int keysize = 16; randData(&skey, keysize); data_entry key(skey, keysize, false); char* sfield = NULL; int fieldsize = 128; char* svalue = NULL; int valuesize = 128; map<data_entry*, data_entry*> field_values; for(int i = 0; i < 100; i++) { randData(&svalue, valuesize); data_entry* value = new data_entry(svalue, valuesize, false); randData(&sfield, fieldsize); data_entry* field = new data_entry(sfield, fieldsize, false); field_values.insert(make_pair(field, value)); int resultcode = tairClient.hset(TairClientRDBTest::default_namespace, key, *field, *value, NOT_CARE_EXPIRE, NOT_CARE_VERSION); ASSERT_EQ(resultcode, TAIR_RETURN_SUCCESS); } { data_entry* field_del = NULL; map<data_entry*, data_entry*>::iterator iter; for(iter = field_values.begin(); iter != field_values.end(); iter++) { field_del = iter->first; break; } int resultcode = tairClient.hdel(TairClientRDBTest::default_namespace, key, *field_del, 5, NOT_CARE_VERSION); ASSERT_EQ(resultcode,TAIR_RETURN_SUCCESS); } sleep(2); { map<data_entry *, data_entry *> field_values_get; int resultcode = tairClient.hgetall(TairClientRDBTest::default_namespace, key, field_values_get); ASSERT_EQ(resultcode, TAIR_RETURN_SUCCESS); ASSERT_EQ(99, field_values_get.size()); map<data_entry *, data_entry *>::iterator iter; for(iter = field_values_get.begin(); iter != field_values_get.end(); iter++) { data_entry* tmp_f = iter->first; data_entry* tmp_v = iter->second; if (tmp_f) { delete tmp_f; } if (tmp_v) { delete tmp_v; } } field_values_get.clear(); } sleep(4); { int resultcode = tairClient.remove(TairClientRDBTest::default_namespace, key); ASSERT_EQ(resultcode, TAIR_RETURN_DATA_NOT_EXIST); if (skey) { free(skey); } map<data_entry *, data_entry *>::iterator iter; for(iter = field_values.begin(); iter != field_values.end(); iter++) { data_entry* tmp_f = iter->first; data_entry* tmp_v = iter->second; if (tmp_f) { char* buff = tmp_f->get_data(); if (buff) { free(buff); } delete tmp_f; } if (tmp_v) { char* buff = tmp_v->get_data(); if (buff) { free(buff); } delete tmp_v; } } field_values.clear(); } }
TEST_F(TairClientRDBTest, test_hdel_version) { { //version char* skey = NULL; int keysize = 16; randData(&skey, keysize); data_entry key(skey, keysize, false); int code = tairClient.remove(TairClientRDBTest::default_namespace, key); ASSERT_EQ(code, TAIR_RETURN_DATA_NOT_EXIST); map<data_entry *, data_entry *> field_values; for(int i = 0; i < 100; i++) { char* svalue = NULL; randData(&svalue, 128); data_entry* value = new data_entry(svalue, 128, false); char* sfield = NULL; randData(&sfield, 128); data_entry* field = new data_entry(sfield, 128, false); field_values.insert(make_pair(field, value)); int resultcode = tairClient.hset(TairClientRDBTest::default_namespace, key, *field, *value, NOT_CARE_EXPIRE, NOT_CARE_VERSION); ASSERT_EQ(resultcode, TAIR_RETURN_SUCCESS); } { map<data_entry *, data_entry *> field_values_get; int resultcode = tairClient.hgetall(TairClientRDBTest::default_namespace, key, field_values_get); ASSERT_EQ(resultcode, TAIR_RETURN_SUCCESS); ASSERT_EQ(100, field_values_get.size()); map<data_entry*, data_entry*>::iterator iter; for(iter = field_values_get.begin(); iter != field_values_get.end(); iter++) { data_entry* tmp_field = iter->first; data_entry* tmp_value = iter->second; if (tmp_field) { delete tmp_field; } if (tmp_value) { delete tmp_value; } } field_values_get.clear(); } { { data_entry temp("zhangsan", 8, true); int resultcode = tairClient.hdel(TairClientRDBTest::default_namespace, key, temp, NOT_CARE_EXPIRE, NOT_CARE_VERSION); ASSERT_EQ(resultcode, TAIR_RETURN_DATA_NOT_EXIST); } map<data_entry*, data_entry*>::iterator iter; data_entry* field_del[2]; { int index = 0; for(iter = field_values.begin(); iter != field_values.end() && index < 2; iter++, index ++) { field_del[index] = iter->first; } int resultcode = tairClient.hdel(TairClientRDBTest::default_namespace, key, *field_del[0], NOT_CARE_EXPIRE, 99); ASSERT_EQ(resultcode, TAIR_RETURN_VERSION_ERROR); resultcode = tairClient.hdel(TairClientRDBTest::default_namespace, key, *field_del[0], NOT_CARE_EXPIRE, 101); ASSERT_EQ(resultcode, TAIR_RETURN_VERSION_ERROR); resultcode = tairClient.hdel(TairClientRDBTest::default_namespace, key, *field_del[0], NOT_CARE_EXPIRE, 100); ASSERT_EQ(resultcode, TAIR_RETURN_SUCCESS); resultcode = tairClient.hdel(TairClientRDBTest::default_namespace, key, *field_del[1], NOT_CARE_EXPIRE, NOT_CARE_VERSION); ASSERT_EQ(resultcode, TAIR_RETURN_SUCCESS); } int i = 2; for(; iter != field_values.end(); iter++, i++) { int resultcode = tairClient.hdel(TairClientRDBTest::default_namespace, key, *(iter->first), NOT_CARE_EXPIRE, 100 + i); ASSERT_EQ(resultcode, TAIR_RETURN_SUCCESS); } int resultcode = tairClient.hdel(TairClientRDBTest::default_namespace, key, *(field_del[0]), NOT_CARE_EXPIRE, NOT_CARE_VERSION); ASSERT_EQ(resultcode, TAIR_RETURN_DATA_NOT_EXIST); resultcode = tairClient.remove(TairClientRDBTest::default_namespace, key); ASSERT_EQ(resultcode, TAIR_RETURN_DATA_NOT_EXIST); if (skey) { free(skey); } map<data_entry*, data_entry*>::iterator iter_x; for(iter_x = field_values.begin(); iter_x != field_values.end(); iter_x++) { data_entry* tmp_field = iter_x->first; data_entry* tmp_value = iter_x->second; if (tmp_field) { char* buff = tmp_field->get_data(); if (buff) { free(buff); } delete tmp_field; } if (tmp_value) { char* buff = tmp_value->get_data(); if (buff) { free(buff); } delete tmp_value; } } field_values.clear(); } } }
//////////////////////////////////////////////////////////////////////////////// // Main program //////////////////////////////////////////////////////////////////////////////// int main(int argc, char **argv){ const unsigned int OPT_N_MAX = 512; unsigned int useDoublePrecision; printf("[binomialOptions]\n"); int devID = cutilDeviceInit(argc, argv); if (devID < 0) { printf("exiting...\n"); cutilExit(argc, argv); exit(0); } cutilSafeCall(cudaGetDevice(&devID)); cudaDeviceProp deviceProp; cutilSafeCall(cudaGetDeviceProperties(&deviceProp, devID)); char *precisionChoice; cutGetCmdLineArgumentstr(argc, (const char **)argv, "type", &precisionChoice); if(precisionChoice == NULL) { useDoublePrecision = 0; } else { if(!strcasecmp(precisionChoice, "double")) useDoublePrecision = 1; else useDoublePrecision = 0; } printf(useDoublePrecision ? "Using double precision...\n" : "Using single precision...\n"); const int OPT_N = deviceEmulation() ? 1 : OPT_N_MAX; TOptionData optionData[OPT_N_MAX]; float callValueBS[OPT_N_MAX], callValueGPU[OPT_N_MAX], callValueCPU[OPT_N_MAX]; double sumDelta, sumRef, gpuTime, errorVal; unsigned int hTimer; int i; cutilCheckError( cutCreateTimer(&hTimer) ); int version = deviceProp.major * 10 + deviceProp.minor; if(useDoublePrecision && version < 13){ printf("Double precision is not supported.\n"); return 0; } printf("Generating input data...\n"); //Generate options set srand(123); for(i = 0; i < OPT_N; i++){ optionData[i].S = randData(5.0f, 30.0f); optionData[i].X = randData(1.0f, 100.0f); optionData[i].T = randData(0.25f, 10.0f); optionData[i].R = 0.06f; optionData[i].V = 0.10f; BlackScholesCall(callValueBS[i], optionData[i]); } printf("Running GPU binomial tree...\n"); cutilSafeCall( cudaThreadSynchronize() ); cutilCheckError( cutResetTimer(hTimer) ); cutilCheckError( cutStartTimer(hTimer) ); if(useDoublePrecision) binomialOptions_SM13(callValueGPU, optionData, OPT_N); else binomialOptions_SM10(callValueGPU, optionData, OPT_N); cutilSafeCall( cudaThreadSynchronize() ); cutilCheckError( cutStopTimer(hTimer) ); gpuTime = cutGetTimerValue(hTimer); printf("Options count : %i \n", OPT_N); printf("Time steps : %i \n", NUM_STEPS); printf("binomialOptionsGPU() time: %f msec\n", gpuTime); printf("Options per second : %f \n", OPT_N / (gpuTime * 0.001)); printf("Running CPU binomial tree...\n"); for(i = 0; i < OPT_N; i++) binomialOptionsCPU(callValueCPU[i], optionData[i]); printf("Comparing the results...\n"); sumDelta = 0; sumRef = 0; printf("GPU binomial vs. Black-Scholes\n"); for(i = 0; i < OPT_N; i++){ sumDelta += fabs(callValueBS[i] - callValueGPU[i]); sumRef += fabs(callValueBS[i]); } if(sumRef >1E-5) printf("L1 norm: %E\n", sumDelta / sumRef); else printf("Avg. diff: %E\n", sumDelta / (double)OPT_N); printf("CPU binomial vs. Black-Scholes\n"); sumDelta = 0; sumRef = 0; for(i = 0; i < OPT_N; i++){ sumDelta += fabs(callValueBS[i]- callValueCPU[i]); sumRef += fabs(callValueBS[i]); } if(sumRef >1E-5) printf("L1 norm: %E\n", sumDelta / sumRef); else printf("Avg. diff: %E\n", sumDelta / (double)OPT_N); printf("CPU binomial vs. GPU binomial\n"); sumDelta = 0; sumRef = 0; for(i = 0; i < OPT_N; i++){ sumDelta += fabs(callValueGPU[i] - callValueCPU[i]); sumRef += callValueCPU[i]; } if(sumRef > 1E-5) printf("L1 norm: %E\n", errorVal = sumDelta / sumRef); else printf("Avg. diff: %E\n", errorVal = sumDelta / (double)OPT_N); printf("Shutting down...\n"); printf("\n[binomialOptions] - Test Summary:\n"); printf((errorVal < 5e-4) ? "PASSED\n" : "FAILED\n"); cutilCheckError( cutDeleteTimer(hTimer) ); cudaThreadExit(); cutilExit(argc, argv); }
//////////////////////////////////////////////////////////////////////////////// // Main program //////////////////////////////////////////////////////////////////////////////// int main(int argc, char **argv) { printf("[%s] - Starting...\n", argv[0]); cudaDeviceProp deviceProp; int devID = findCudaDevice(argc, (const char **)argv); checkCudaErrors(cudaGetDeviceProperties(&deviceProp, devID)); if (((deviceProp.major << 4) + deviceProp.minor) < 0x20) { fprintf(stderr, "binomialOptions requires Compute Capability of SM 2.0 or higher to run.\n"); cudaDeviceReset(); exit(EXIT_WAIVED); } const int OPT_N = MAX_OPTIONS; TOptionData optionData[MAX_OPTIONS]; real callValueBS[MAX_OPTIONS], callValueGPU[MAX_OPTIONS], callValueCPU[MAX_OPTIONS]; real sumDelta, sumRef, gpuTime, errorVal; StopWatchInterface *hTimer = NULL; int i; sdkCreateTimer(&hTimer); printf("Generating input data...\n"); //Generate options set srand(123); for (i = 0; i < OPT_N; i++) { optionData[i].S = randData(5.0f, 30.0f); optionData[i].X = randData(1.0f, 100.0f); optionData[i].T = randData(0.25f, 10.0f); optionData[i].R = 0.06f; optionData[i].V = 0.10f; BlackScholesCall(callValueBS[i], optionData[i]); } printf("Running GPU binomial tree...\n"); checkCudaErrors(cudaDeviceSynchronize()); sdkResetTimer(&hTimer); sdkStartTimer(&hTimer); binomialOptionsGPU(callValueGPU, optionData, OPT_N); checkCudaErrors(cudaDeviceSynchronize()); sdkStopTimer(&hTimer); gpuTime = sdkGetTimerValue(&hTimer); printf("Options count : %i \n", OPT_N); printf("Time steps : %i \n", NUM_STEPS); printf("binomialOptionsGPU() time: %f msec\n", gpuTime); printf("Options per second : %f \n", OPT_N / (gpuTime * 0.001)); printf("Running CPU binomial tree...\n"); for (i = 0; i < OPT_N; i++) { binomialOptionsCPU(callValueCPU[i], optionData[i]); } printf("Comparing the results...\n"); sumDelta = 0; sumRef = 0; printf("GPU binomial vs. Black-Scholes\n"); for (i = 0; i < OPT_N; i++) { sumDelta += fabs(callValueBS[i] - callValueGPU[i]); sumRef += fabs(callValueBS[i]); } if (sumRef >1E-5) { printf("L1 norm: %E\n", (double)(sumDelta / sumRef)); } else { printf("Avg. diff: %E\n", (double)(sumDelta / (real)OPT_N)); } printf("CPU binomial vs. Black-Scholes\n"); sumDelta = 0; sumRef = 0; for (i = 0; i < OPT_N; i++) { sumDelta += fabs(callValueBS[i]- callValueCPU[i]); sumRef += fabs(callValueBS[i]); } if (sumRef >1E-5) { printf("L1 norm: %E\n", sumDelta / sumRef); } else { printf("Avg. diff: %E\n", (double)(sumDelta / (real)OPT_N)); } printf("CPU binomial vs. GPU binomial\n"); sumDelta = 0; sumRef = 0; for (i = 0; i < OPT_N; i++) { sumDelta += fabs(callValueGPU[i] - callValueCPU[i]); sumRef += callValueCPU[i]; } if (sumRef > 1E-5) { printf("L1 norm: %E\n", errorVal = sumDelta / sumRef); } else { printf("Avg. diff: %E\n", (double)(sumDelta / (real)OPT_N)); } printf("Shutting down...\n"); sdkDeleteTimer(&hTimer); // cudaDeviceReset causes the driver to clean up all state. While // not mandatory in normal operation, it is good practice. It is also // needed to ensure correct operation when the application is being // profiled. Calling cudaDeviceReset causes all profile data to be // flushed before the application exits cudaDeviceReset(); printf("\nNOTE: The CUDA Samples are not meant for performance measurements. Results may vary when GPU Boost is enabled.\n\n"); if (errorVal > 5e-4) { printf("Test failed!\n"); exit(EXIT_FAILURE); } printf("Test passed\n"); exit(EXIT_SUCCESS); }