// Compute an expensive hash using multiple applications of cityhash LL hash(LL key) { uint32_t lo = (LL) CityHash32((char *) &key, 4); uint32_t hi = (LL) CityHash32((char *) &lo, 4); LL hi_LL = (LL) hi; return ((hi_LL << 32) | lo); }
void *reader( void *ptr) { struct timespec start, end; int tid = *((int *) ptr); uint64_t seed = 0xdeadbeef + tid; int sum = 0, i; /** < The node and lock to use in an iteration */ int node_id, lock_id; /** < Total number of iterations (for measurement) */ int num_iters = 0; clock_gettime(CLOCK_REALTIME, &start); while(1) { if(num_iters == ITERS_PER_MEASUREMENT) { clock_gettime(CLOCK_REALTIME, &end); double seconds = (end.tv_sec - start.tv_sec) + (double) (end.tv_nsec - start.tv_nsec) / GHZ_CPS; printf("Reader thread %d: rate = %.2f M/s. Sum = %d\n", tid, num_iters / (1000000 * seconds), sum); num_iters = 0; clock_gettime(CLOCK_REALTIME, &start); } node_id = fastrand(&seed) & NUM_NODES_; lock_id = node_id & NUM_LOCKS_; #if USE_SKIP == 1 while(pthread_spin_trylock(&locks[lock_id].lock) == EBUSY) { lock_id = (lock_id + 1) & NUM_LOCKS_; } #else pthread_spin_lock(&locks[lock_id].lock); /** < Critical section begin */ if(nodes[node_id].b != nodes[node_id].a + 1) { red_printf("Invariant violated\n"); } #endif for(i = 0; i < WRITER_COMPUTE; i ++) { sum += CityHash32((char *) &nodes[node_id].a, 4); sum += CityHash32((char *) &nodes[node_id].b, 4); } /** < Critical section end */ pthread_spin_unlock(&locks[lock_id].lock); num_iters ++; } }
int lookup(const char *url){ /* please insert your codes about data lookup here */ char buf[1001]; strncpy(buf,url,sizeof(buf)); //pre operate url int token_count = 0; char * temp_buf[100]; char *pch = strtok(buf, "/"); while (pch != NULL) { temp_buf[token_count] = pch; ++token_count; pch = strtok (NULL, "/"); } //magic int a = (CityHash32(temp_buf[1%token_count], strlen(temp_buf[1%token_count])) % 997); int b = (CityHash32(temp_buf[3%token_count], strlen(temp_buf[3%token_count])) % 997); int j = token_count; if(j > 9) j = 10; //Search struct Node* itor = magic_table[a][b][j-1]; for( ; itor != NULL; itor = itor->magic_pointer) { if(j != itor->token) continue; int i = 0; for(; i < itor->token; ++i) { if(0 != strcmp(itor->data[i], temp_buf[i])) break; } if(i == itor->token) return itor->value; } return -1; }
void *writer( void *ptr) { struct timespec start, end; int tid = *((int *) ptr); uint64_t seed = 0xdeadbeef + tid; int sum = 0; /** < The node and lock to use in an iteration */ int i, j, node_id, lock_id; /** < Total number of iterations (for measurement) */ int num_iters = 0; clock_gettime(CLOCK_REALTIME, &start); while(1) { if(num_iters == ITERS_PER_MEASUREMENT) { clock_gettime(CLOCK_REALTIME, &end); double seconds = (end.tv_sec - start.tv_sec) + (double) (end.tv_nsec - start.tv_nsec) / GHZ_CPS; node_id = fastrand(&seed) & NUM_NODES_; red_printf("Writer thread %d: rate = %.2f M/s. " "Random node: (%lld, %lld)\n", tid, num_iters / (1000000 * seconds), nodes[node_id].a, nodes[node_id].b); num_iters = 0; clock_gettime(CLOCK_REALTIME, &start); } node_id = fastrand(&seed) & NUM_NODES_; lock_id = node_id & NUM_LOCKS_; for(j = 0; j < NUM_WRITER_LOCKS; j ++) { int lock_index = (lock_id + (j * WRITER_LOCK_STRIDE)) & NUM_LOCKS_; pthread_spin_lock(&locks[lock_index].lock); } /** < Update node.a and node.b after some expensive computation */ for(i = 0; i < WRITER_COMPUTE; i ++) { nodes[node_id].a = CityHash32((char *) &nodes[node_id].a, 4); } nodes[node_id].b = nodes[node_id].a + 1; for(j = 0; j < NUM_WRITER_LOCKS; j ++) { int lock_index = (lock_id + (j * WRITER_LOCK_STRIDE)) & NUM_LOCKS_; pthread_spin_unlock(&locks[lock_index].lock); } num_iters ++; } }
void *writer( void *ptr) { struct timespec start, end; int tid = *((int *) ptr); uint64_t seed = 0xdeadbeef + tid; int sum = 0; /** < The node and lock to use in an iteration */ int i, node_id, lock_id; /** < Total number of iterations (for measurement) */ int num_iters = 0; clock_gettime(CLOCK_REALTIME, &start); while(1) { if(num_iters == ITERS_PER_MEASUREMENT) { clock_gettime(CLOCK_REALTIME, &end); double seconds = (end.tv_sec - start.tv_sec) + (double) (end.tv_nsec - start.tv_nsec) / GHZ_CPS; node_id = fastrand(&seed) & NUM_NODES_; red_printf("Writer thread %d: rate = %.2f M/s. " "Random node: (%lld, %lld)\n", tid, num_iters / (1000000 * seconds), nodes[node_id].a, nodes[node_id].b); num_iters = 0; clock_gettime(CLOCK_REALTIME, &start); } node_id = fastrand(&seed) & NUM_NODES_; lock_id = node_id & NUM_LOCKS_; locks[lock_id].lock ++; /** < version store #1 --> node stores */ asm volatile("" ::: "memory"); /** < Update node.a and node.b after some expensive computation */ for(i = 0; i < WRITER_COMPUTE; i ++) { nodes[node_id].a = CityHash32((char *) &nodes[node_id].a, 4); } nodes[node_id].b = nodes[node_id].a + 1; /** < node stores --> version store #2 */ asm volatile("" ::: "memory"); locks[lock_id].lock ++; num_iters ++; } }
Hash Hash::CityHash(const void* data, uint32_t sz) { hash_type newHash; //use a different seed on the first 4 bytes than on the last byte uint128 someSeed(503,127); ((uint128*)&newHash)[0] = CityHash128WithSeed((const char*)data, sz, someSeed); newHash[4] = CityHash32((const char*)data, sz); return newHash; }
int sceJpegGetOutputInfo(u32 jpegAddr, int jpegSize, u32 colourInfoAddr, int dhtMode) { ERROR_LOG_REPORT(ME, "sceJpegGetOutputInfo(%i, %i, %i, %i)", jpegAddr, jpegSize, colourInfoAddr, dhtMode); int w = 0, h = 0, actual_components = 0; if (!Memory::IsValidAddress(jpegAddr)) { ERROR_LOG(ME, "sceJpegGetOutputInfo: Bad JPEG address 0x%08x", jpegAddr); return getYCbCrBufferSize(0, 0); } else // Memory address is good { // But data may not be...so check it u8 *buf = Memory::GetPointer(jpegAddr); unsigned char *jpegBuf = jpgd::decompress_jpeg_image_from_memory(buf, jpegSize, &w, &h, &actual_components, 3); if (actual_components != 3) { // The assumption that the image was RGB was wrong... // Try again. int components = actual_components; jpegBuf = jpgd::decompress_jpeg_image_from_memory(buf, jpegSize, &w, &h, &actual_components, components); } if (jpegBuf == NULL) { ERROR_LOG(ME, "sceJpegGetOutputInfo: Bad JPEG data"); return getYCbCrBufferSize(0, 0); } } // Buffer to store info about the color space in use. // - Bits 24 to 32 (Always empty): 0x00 // - Bits 16 to 24 (Color mode): 0x00 (Unknown), 0x01 (Greyscale) or 0x02 (YCbCr) // - Bits 8 to 16 (Vertical chroma subsampling value): 0x00, 0x01 or 0x02 // - Bits 0 to 8 (Horizontal chroma subsampling value): 0x00, 0x01 or 0x02 if (Memory::IsValidAddress(colourInfoAddr)) Memory::Write_U32(0x00020202, colourInfoAddr); #ifdef JPEG_DEBUG char jpeg_fname[256]; u8 *jpegBuf = Memory::GetPointer(jpegAddr); uint32 jpeg_cityhash = CityHash32((const char *)jpegBuf, jpegSize); sprintf(jpeg_fname, "Jpeg\\%X.jpg", jpeg_cityhash); FILE *wfp = fopen(jpeg_fname, "wb"); fwrite(jpegBuf, 1, jpegSize, wfp); fclose(wfp); #endif //JPEG_DEBUG return getYCbCrBufferSize(w, h); }
int main(int argc, char **argv) { /** < num_pkts is passed as a command-line flag */ assert(argc == 2); int num_pkts = atoi(argv[1]); int sum = 0; int *A = (int *) malloc(num_pkts * sizeof(int)); /** < We can only get full execution measurements */ struct timespec start, end; double tot, iter_us; int iter = 0, j = 0; for(iter = 0; iter < ITERS; iter ++) { /** < Start a timer */ clock_gettime(CLOCK_REALTIME, &start); /** < Write input data into A */ for(j = 0; j < num_pkts; j ++) { A[j] = j + iter + 1; } for(j = 0; j < num_pkts; j ++) { sum += CityHash32((char *) &A[j], 4); } /** < Stop timer */ clock_gettime(CLOCK_REALTIME, &end); iter_us = (end.tv_sec - start.tv_sec) * 1000000 + (double) (end.tv_nsec - start.tv_nsec) / 1000; printf("\tIter %d: %.2f us. Per packet: %.2f ns, sum = %d\n", iter, iter_us, iter_us * 1000 / num_pkts, sum); tot += iter_us; } printf("Average %.2f us\n", tot / ITERS); free(A); printf("Done\n"); return 0; }
uint32_t SkCityHash::Compute32(const char *data, size_t size) { return CityHash32(data, size); }
void input() { char buf[1001]; int i; while (1){ i = read_line(buf); /* end of the input data */ if(i <= 0) { break; } /* there are input data available, and you may want to insert your codes here */ data_current = (struct Node *)malloc(sizeof(struct Node)); if(data_current == NULL) exit(EXIT_FAILURE); data_current->next = NULL; data_current->magic_pointer = NULL; data_current->token = 0; //take token char* front = strtok(buf, " "); char* back = strtok (NULL, " "); //value data_current->value = atoi(back); //count front token char * temp_buf[100]; char *pch = strtok(front , "/"); while (pch != NULL) { temp_buf[data_current->token] = pch; ++data_current->token; pch = strtok (NULL, "/"); } //store data data_current->data = (char **)malloc(data_current->token*sizeof(char *)); int token_count = 0; for(token_count = 0; token_count < data_current->token; ++token_count) { data_current->data[token_count] = (char *)malloc((strlen(temp_buf[token_count])+1)*sizeof(char)); strcpy(data_current->data[token_count], temp_buf[token_count]); data_current->data[token_count][strlen(temp_buf[token_count])] = '\0'; } //magic int a = (CityHash32(data_current->data[1%data_current->token], strlen(data_current->data[1%data_current->token])) % 997); int b = (CityHash32(data_current->data[3%data_current->token], strlen(data_current->data[3%data_current->token])) % 997); int j = 0; j = data_current->token; if(j > 9) j = 10; if(magic_table[a][b][j-1] == NULL) { magic_table[a][b][j-1] = data_current; } else { struct Node* ptr = magic_table[a][b][j-1]; while(ptr->magic_pointer != NULL) ptr = ptr->magic_pointer; ptr->magic_pointer = data_current; } //chain linked list if(data_head == NULL) data_head = data_current; else data_prev->next = data_current; data_prev = data_current; } }
static size_t compute(const char* buffer, size_t length) { return static_cast<size_t>(CityHash32(buffer, length)); }