/** * Writes a matrix to a generic stream */ void write_matrix(const Matrix *m, FILE *f) { write_magic(f); write_dims(f, m->rows, m->columns); int i; for (i = 0; i < m->rows; ++i) { write_row(f, m->data[i], m->columns); } }
void generate(FILE *f, const int rows, const int cols) { write_magic(f); write_dims(f, rows, cols); int i; int j; DataType row[cols]; for (i = 0; i < rows; ++i) { for (j = 0; j < cols; ++j) { /* row[j] = ((int) random()) % kValueLimit; */ #ifdef MATRIX_OF_DOUBLES row[j] = random() / ((DataType) RAND_MAX); #else row[j] = ((int) random()) % kValueLimit; #endif } write_row(f, row, cols); } }
/* * static void *alloc_mem * * DESCRIPTION: * * Allocate space for bytes inside of an already open memory pool. * * RETURNS: * * Success - Pointer to the address to use. * * Failure - NULL * * ARGUMENTS: * * mp_p <-> Pointer to the memory pool. If NULL then it will do a * normal malloc. * * byte_size -> Number of bytes to allocate in the pool. Must be >0. * * error_p <- Pointer to integer which, if not NULL, will be set with * a mpool error code. */ static void *alloc_mem(mpool_t *mp_p, const unsigned long byte_size, int *error_p) { unsigned long size, fence; void *addr; /* make sure we have enough bytes */ if (byte_size < MIN_ALLOCATION) { size = MIN_ALLOCATION; } else { size = byte_size; } if (BIT_IS_SET(mp_p->mp_flags, MPOOL_FLAG_NO_FREE)) { fence = 0; } else { fence = FENCE_SIZE; } /* get our free space + the space for the fence post */ addr = get_space(mp_p, size + fence, error_p); if (addr == NULL) { /* error_p set in get_space */ return NULL; } if (! BIT_IS_SET(mp_p->mp_flags, MPOOL_FLAG_NO_FREE)) { write_magic((char *)addr + size); } /* maintain our stats */ mp_p->mp_alloc_c++; mp_p->mp_user_alloc += size; if (mp_p->mp_user_alloc > mp_p->mp_max_alloc) { mp_p->mp_max_alloc = mp_p->mp_user_alloc; } SET_POINTER(error_p, MPOOL_ERROR_NONE); return addr; }
int cr_main(int argc, char** argv) { struct { uint32_t m_size; uint8_t m_filt; uint8_t m_prec; } __attribute__((packed)) block_header; const char* src_name = "<stdin>"; const char* dst_name = "<stdout>"; FILE* src_file; FILE* dst_file; data_block_t ib = INITIAL_BLOCK; data_block_t ob = INITIAL_BLOCK; data_block_t* xb; data_block_t* yb; uint32_t src_size; uint32_t dst_size; int filt = 0; int enc; data_block_t dic_xb = INITIAL_BLOCK; data_block_t dic_yb = INITIAL_BLOCK; int nword; struct timeval time_start; struct timeval time_end; double cost_time; gettimeofday(&time_start, NULL); src_file = stdin; dst_file = stdout; #if defined(_WIN32) || defined(_WIN64) /* we need to set stdin/stdout to binary mode under windows */ setmode(fileno(stdin), O_BINARY); setmode(fileno(stdout), O_BINARY); #endif /* reset global models for compressing/decompressing */ reset_models(); /* process arguments */ if((argc = cr_process_arguments(argc, argv)) == 0) { return -1; } /* start! */ fprintf(stderr, "%s\n", cr_start_info); if(argc >=2 && argc <= 4 && strcmp(argv[1], "e") == 0) { /* encode */ enc = 1; if(argc >= 3) src_name = argv[2], src_file = fopen(src_name, "rb"); if(argc >= 4) dst_name = argv[3], dst_file = fopen(dst_name, "wb"); if(src_file == stdin) { /* copy input data to temporary file, since stdin doesn't support rewind() */ data_block_reserve(&ib, 1048576); src_file = tmpfile(); while((ib.m_size = fread(ib.m_data, 1, ib.m_capacity, stdin)) > 0) { fwrite(ib.m_data, 1, ib.m_size, src_file); } data_block_destroy(&ib); rewind(src_file); ib = INITIAL_BLOCK; } if(src_file != NULL && dst_file != NULL) { write_magic(dst_file); fprintf(stderr, "compressing %s to %s, block_size = %uMB...\n", src_name, dst_name, cr_split_size / 1048576); /* build static dictionary */ fprintf(stderr, "%s\n", "-> building static dictionary..."); dicpick(src_file, &dic_xb); rewind(src_file); nword = dictionary_load((char*)dic_xb.m_data, 1); /* encode static dictionary */ dic_lcp_encode(&dic_xb); lzencode(&dic_xb, &dic_yb, 0); reset_models(); fprintf(stderr, "added %d words to dictionary, compressed size = %u bytes\n", nword, dic_yb.m_size); /* write static dictionary to dst_file */ fwrite(&dic_yb.m_size, sizeof(dic_yb.m_size), 1, dst_file); fwrite( dic_yb.m_data, 1, dic_yb.m_size, dst_file); data_block_destroy(&dic_xb); data_block_destroy(&dic_yb); while(!ferror(src_file) && !ferror(dst_file) && !feof(src_file)) { xb = &ib; yb = &ob; data_block_resize(xb, cr_split_size); /* read blocks */ xb->m_size = fread(xb->m_data, 1, cr_split_size, src_file); /* precompress with filters */ if(cr_filt_enable) { filt = filter_inplace(xb->m_data, xb->m_size, FILTER_ENC); } /* encode */ data_block_resize(yb, 0); dictionary_encode(xb, yb); if(!cr_prec_enable) { swap_xyblock(&xb, &yb); data_block_resize(yb, 0); lzencode(xb, yb, !ferror(stderr)); } /* write blocks */ if(yb->m_size > 0) { block_header.m_size = yb->m_size; block_header.m_filt = filt; block_header.m_prec = cr_prec_enable; fwrite(&block_header, sizeof(block_header), 1, dst_file); fwrite(yb->m_data, 1, yb->m_size, dst_file); } } if(ferror(src_file) || ferror(dst_file)) { perror("ferror()"); return -1; } } else { perror("fopen()"); return -1; } src_size = ftell(src_file); dst_size = ftell(dst_file); fclose(src_file); fclose(dst_file); } else if(argc >= 2 && argc <= 4 && strcmp(argv[1], "d") == 0) { /* decode */ enc = 0; if(argc >= 3) src_name = argv[2], src_file = fopen(src_name, "rb"); if(argc >= 4) dst_name = argv[3], dst_file = fopen(dst_name, "wb"); if(src_file == stdin) { /* copy input data to temporary file, since stdin doesn't support rewind() */ data_block_reserve(&ib, 1048576); src_file = tmpfile(); while((ib.m_size = fread(ib.m_data, 1, ib.m_capacity, stdin)) > 0) { fwrite(ib.m_data, 1, ib.m_size, src_file); } data_block_destroy(&ib); rewind(src_file); ib = INITIAL_BLOCK; } if(src_file != NULL && dst_file != NULL) { if(!check_magic(src_file)) { fprintf(stderr, "%s\n", "check_magic() failed."); fclose(src_file); fclose(dst_file); return -1; } fprintf(stderr, "decompressing %s to %s...\n", src_name, dst_name); /* decode static dictionary */ fprintf(stderr, "%s\n", "-> decoding static dictionary..."); /* read size of static dictionary from src_file */ fread(&dic_yb.m_size, sizeof(dic_yb.m_size), 1, src_file); /* read static dictionary from src_file */ data_block_resize(&dic_yb, dic_yb.m_size); fread(dic_yb.m_data, 1, dic_yb.m_size, src_file); /* decode static dictionary */ lzdecode(&dic_yb, &dic_xb, 0); reset_models(); dic_lcp_decode(&dic_xb); dictionary_load((char*)dic_xb.m_data, 0); data_block_destroy(&dic_xb); data_block_destroy(&dic_yb); while(!ferror(src_file) && !ferror(dst_file) && !feof(src_file)) { xb = &ib; yb = &ob; /* read blocks */ if(fread(&block_header, sizeof(block_header), 1, src_file) != 1) { break; } data_block_resize(yb, block_header.m_size); yb->m_size = fread(yb->m_data, 1, yb->m_size, src_file); /* decode */ if(!block_header.m_prec) { data_block_resize(xb, 0); lzdecode(yb, xb, !ferror(stderr)); swap_xyblock(&xb, &yb); } data_block_resize(xb, 0); dictionary_decode(yb, xb, dst_file); /* precompress with filters */ if(block_header.m_filt) { filter_inplace(xb->m_data, xb->m_size, FILTER_DEC); } /* write blocks */ if(xb->m_size > 0) { fwrite(xb->m_data, 1, xb->m_size, dst_file); } } if(ferror(src_file) || ferror(dst_file)) { perror("ferror()"); return -1; } } else { perror("fopen()"); return -1; } src_size = ftell(src_file); dst_size = ftell(dst_file); fclose(src_file); fclose(dst_file); } else { /* bad argument! */ fprintf(stderr, "%s\n", cr_usage_info); return -1; } data_block_destroy(&ib); data_block_destroy(&ob); gettimeofday(&time_end, NULL); cost_time = (time_end.tv_sec - time_start.tv_sec) + (time_end.tv_usec - time_start.tv_usec) / 1000000.0; fprintf(stderr, "%u bytes => %u bytes\n\n", src_size, dst_size); if(enc) { fprintf(stderr, "encode-speed: %.3lf MB/s\n", src_size / 1048576 / cost_time); fprintf(stderr, "cost-time: %.3lf s\n", cost_time); fprintf(stderr, "compress-ratio: %.3lf\n", (double)dst_size / src_size); fprintf(stderr, "bpb: %.3lf\n", (double)dst_size / src_size * 8); } else { fprintf(stderr, "decode-speed: %.3lf MB/s\n", dst_size / 1048576 / cost_time); fprintf(stderr, "cost-time: %.3lf s\n", cost_time); fprintf(stderr, "compress-ratio: %.3lf\n", (double)src_size / dst_size); fprintf(stderr, "bpb: %.3lf\n", (double)src_size / dst_size * 8); } return 0; }