Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Parallelized the CALIC implementation #3

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions refs/calic/AdaptiveModel.cc
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "Arithm.h"
#include <stdio.h>
#include <string.h>

AdaptiveModel::AdaptiveModel() {
numb_symb = 0;
Expand Down
2 changes: 1 addition & 1 deletion refs/calic/Arithm.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

#include <stdlib.h>
#include <stdio.h>

#include <omp.h>
#define MaxSymbols 257

#define CodeValueBits 16
Expand Down
16 changes: 11 additions & 5 deletions refs/calic/ArithmeticDecoder.cc
Original file line number Diff line number Diff line change
Expand Up @@ -56,15 +56,21 @@ void ArithmeticDecoder::Input_Byte() {
}

void ArithmeticDecoder::update() {
for (;;) {
if (high >= Half) {
if (low >= Half) {

for (;;)
{
if (high >= Half)
{
if (low >= Half)
{
value -= Half;
low -= Half;
high -= Half;
}
else {
if ((low >= FirstQtr) && (high < ThirdQtr)) {
else
{
if ((low >= FirstQtr) && (high < ThirdQtr))
{
value -= FirstQtr; low -= FirstQtr;
high -= FirstQtr;
}
Expand Down
78 changes: 33 additions & 45 deletions refs/calic/BinaryModeModel.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4,41 +4,7 @@
void BinaryModeModel::update(int index) {
// if (incr == 1)
AdaptiveModel::update(index);
/*
else {
int i, cum, symb, new_freq;

if (cum_freq[0] >= MaxFrequency) {
for (cum = 0, i = numb_symb; i >= 0; i--) {
freq[i] = (freq[i] + 1) >> 1;
cum_freq[i] = cum;
cum += freq[i];
}
incr = (incr + 1) / 2;
}

new_freq = freq[index] + incr;
symb = index_to_symb[index];

int tmp_idx, tmp_symb;
for (i = index; new_freq >= freq[i-1] && i > 1; i--) {
freq[i] = freq[i-1];
tmp_symb = index_to_symb[i-1];
index_to_symb[i] = tmp_symb;
symb_to_index[tmp_symb] = i;
}

freq[i] = new_freq;

if (i < index) {
index_to_symb[i] = symb;
symb_to_index[symb] = i;
}

while (i)
cum_freq[--i] += incr;
}
*/
}

void BinaryModeModel::init(int ns) {
Expand All @@ -52,28 +18,50 @@ void ContinuousModeModel::init(int ns) {

int i, cum;

int freqCount = (MaxFrequency * 2 / 4);
int freqCount = (MaxFrequency / 2);
double *freqPercentage = new double[ns];

double sum = 0.0;
for (int i = 0; i < ns; i++) {
freqPercentage[i] = pow(15.0, 0.4 * (double)(-i-1));
sum += freqPercentage[i];
double temp;
#pragma omp parallel for private(temp) reduction(+:sum)
for (int i = 0; i < ns; i++)
{
//prevent false sharing and double indirect addressing overhead using a `temp`orary variable
temp = pow(15.0, 0.4 * (double)(-i-1));
freqPercentage[i] = temp;
sum += temp;
//temp avoids data dependency issues instead of freqPercentage
}

for (int i = 0; i < ns; i++) {
freqPercentage[i] /= sum;
#pragma omp parallel for shared(freqPercentage)
for (int i = 0; i < ns; i++)
{
//Get and store probabiliity
freqPercentage[i] /= sum;
}

for (i = 1; i <= ns; i++) {
for (i = 1; i <= ns; i++)
{

//Absolute frequency
freq[i] = (int)((double)freqCount * freqPercentage[i-1]);
if (freq[i] == 0)
freq[i] = 1;
}

for (cum = 0, i = ns; i >= 0; i--) {
cum_freq[i] = cum;
cum += freq[i];

//Cumulative Frequency => loop dependencies => prefix sum
cum_freq[ns] = 0;
int j;
#pragma omp parallel for schedule(dynamic)
for ( i = ns - 1; i >= 0; i--)
{
cum_freq[i] = 0;
//#pragma omp parallel for reduction is an overhead
for(j=ns;j>=i;j--)
{
cum_freq[i] += freq[j];
}

}

delete [] freqPercentage;
Expand Down
6 changes: 3 additions & 3 deletions refs/calic/CommandLine.cc
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,7 @@ CommandLine::~CommandLine() {
// Otherwise it returns False.
//
//////////////////////////////////////////////////////////////////////
bool CommandLine::getOption(char *str) {

bool CommandLine::getOption(char *str) {
for (int i = 1; i < argc; i++)
if (argv[i][0] == '-' && (strcmp(argv[i] + 1, str) == 0))
return true;
Expand Down Expand Up @@ -90,7 +89,7 @@ bool CommandLine::getParameter(char *str, float *var) {
bool CommandLine::getParameter(char *str, double *var) {

for (int i = 1; i < argc - 1; i++)
if (argv[i][0] == '-' && (strcmp(argv[i] + 1, str) == 0)) {
if (argv[i][0] == '-' && (strcmp(argv[i] + 1, str) == 0)) { //false sharing if parallelized
*var = (double) atof(argv[i+1]);
return true;
}
Expand All @@ -113,6 +112,7 @@ void CommandLine::setArguements(int _argc, char *_argv[]) {
argc = _argc;

argv = new char * [argc];
#pragma omp parallel for
for (int i = 0; i < argc; i++) {
argv[i] = new char [MAX_STR_LEN];
strcpy(argv[i], _argv[i]);
Expand Down
1 change: 1 addition & 0 deletions refs/calic/CommandLine.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <omp.h>

#ifndef MAX_STR_LEN
#define MAX_STR_LEN 64
Expand Down
1 change: 1 addition & 0 deletions refs/calic/ImageCodec.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#define __IMAGECODEC_H__

#include <pnm.h>
#include <string.h>
#include "Arithm.h"
#include "Image.h"

Expand Down
4 changes: 2 additions & 2 deletions refs/calic/Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
ARCH = $(shell uname)

CC = g++ -Wall
CC = g++ -Wall -fopenmp

PROGRAMS = encode decode

Expand Down Expand Up @@ -30,7 +30,7 @@ OBJS = $(ARITHM_OBJS) $(PNM_OBJS) $(MISC_OBJS) $(IMAGECODEC_OBJS)

INCDIR = .

CFLAGS = -O2 -I$(INCDIR) -D$(ARCH)
CFLAGS = -O2 -I$(INCDIR) -D$(ARCH) -fopenmp

.SUFFIXES: .cc .
.cc.o:
Expand Down
5 changes: 4 additions & 1 deletion refs/calic/StatisticalModel.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@

void StatisticalModel::reset() {
for (int i = 0; i < size; i++)
model[i] = modelCount[i] = 0;
{
model[i] = 0;
modelCount[i] = 0;
}
}

int StatisticalModel::write(char *filename) {
Expand Down
Binary file added refs/calic/decode
Binary file not shown.
Binary file added refs/calic/encode
Binary file not shown.
11 changes: 6 additions & 5 deletions refs/calic/encode.cc
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,9 @@ int main(int argc, char *argv[]) {

if (debugLevel > 1) {
ShortImage * errImg = codec->getErrorImage();
int w = errImg->w;;
int w = errImg->w;
int h = errImg->h;

FILE *ofp;

char errorFilename[256];
Expand All @@ -71,9 +71,10 @@ int main(int argc, char *argv[]) {
else {
PGMImage *img = new PGMImage(w, h);

for (int y = 0; y < h; y++)
for (int x = 0; x < w; x++) {
fprintf(ofp, "%d %d %d\n", x, y, errImg->getPixel(x, y));
#pragma omp parallel for
for (int y = 0; y < h; y++)
for (int x = 0; x < w; x++) { //overhead in parallelizing
fprintf(ofp, "%d %d %d\n", x, y, errImg->getPixel(x, y)); //thread safe
img->setPixel(x, y, (errImg->getPixel(x, y) < 0) ? 0 : 255);
}

Expand Down
3,906 changes: 3,906 additions & 0 deletions refs/calic/input/cman.pgm

Large diffs are not rendered by default.

Binary file added refs/calic/input/some-wallpapers-4.pgm
Binary file not shown.
Binary file added refs/calic/output/some-wallpapers-decode.pgm
Binary file not shown.
Binary file added refs/calic/output/some-wallpapers-encode.pgm
Binary file not shown.
2 changes: 1 addition & 1 deletion refs/calic/pnm.cc
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,6 @@ int PPMImage::write(char *filename) {
}

fprintf(fp, "P6\n%d %d\n%d\n", width, height, maxIntensity);

for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
tmp = pixels[i*width + j] << 8;
Expand Down Expand Up @@ -184,6 +183,7 @@ PPMImage *PPMImage::copyArea(int x, int y, int w, int h) {
register int i, j, imgsize;

imgsize = w * h;
#pragma omp parallel for
for (i = 0; i < imgsize; i++)
newimg->pixels[i] = 0;

Expand Down
2 changes: 1 addition & 1 deletion refs/calic/pnm.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

#include <stdio.h>
#include <stdlib.h>

#include <omp.h>
typedef unsigned char byte;
typedef unsigned short int16;

Expand Down