Skip to content

Commit

Permalink
testing this
Browse files Browse the repository at this point in the history
  • Loading branch information
arwin-swapna committed Mar 19, 2024
0 parents commit 0a54d9d
Show file tree
Hide file tree
Showing 107 changed files with 122,815 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"files.associations": {
"stdbool.h": "c"
}
}
11 changes: 11 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
vzip: serial.c
gcc serial.c -lz -o vzip

test:
rm -f video.vzip
./vzip frames
./check.sh

clean:
rm -f vzip video.vzip

8 changes: 8 additions & 0 deletions check.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/bin/bash

if [[ $(diff video.vzip reference.vzip) == "" ]]
then
echo SUCCESS
else
echo FAIL
fi
4 changes: 4 additions & 0 deletions frames/0001.ppm

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions frames/0002.ppm

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions frames/0003.ppm

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions frames/0004.ppm

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions frames/0005.ppm

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions frames/0006.ppm

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions frames/0007.ppm

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions frames/0008.ppm

Large diffs are not rendered by default.

7 changes: 7 additions & 0 deletions frames/0009.ppm

Large diffs are not rendered by default.

10 changes: 10 additions & 0 deletions frames/0010.ppm

Large diffs are not rendered by default.

13 changes: 13 additions & 0 deletions frames/0011.ppm

Large diffs are not rendered by default.

13 changes: 13 additions & 0 deletions frames/0012.ppm

Large diffs are not rendered by default.

179 changes: 179 additions & 0 deletions frames/0013.ppm

Large diffs are not rendered by default.

182 changes: 182 additions & 0 deletions frames/0014.ppm

Large diffs are not rendered by default.

131 changes: 131 additions & 0 deletions frames/0015.ppm

Large diffs are not rendered by default.

230 changes: 230 additions & 0 deletions frames/0016.ppm

Large diffs are not rendered by default.

248 changes: 248 additions & 0 deletions frames/0017.ppm

Large diffs are not rendered by default.

262 changes: 262 additions & 0 deletions frames/0018.ppm

Large diffs are not rendered by default.

262 changes: 262 additions & 0 deletions frames/0019.ppm

Large diffs are not rendered by default.

262 changes: 262 additions & 0 deletions frames/0020.ppm

Large diffs are not rendered by default.

265 changes: 265 additions & 0 deletions frames/0021.ppm

Large diffs are not rendered by default.

265 changes: 265 additions & 0 deletions frames/0022.ppm

Large diffs are not rendered by default.

265 changes: 265 additions & 0 deletions frames/0023.ppm

Large diffs are not rendered by default.

265 changes: 265 additions & 0 deletions frames/0024.ppm

Large diffs are not rendered by default.

265 changes: 265 additions & 0 deletions frames/0025.ppm

Large diffs are not rendered by default.

265 changes: 265 additions & 0 deletions frames/0026.ppm

Large diffs are not rendered by default.

265 changes: 265 additions & 0 deletions frames/0027.ppm

Large diffs are not rendered by default.

265 changes: 265 additions & 0 deletions frames/0028.ppm

Large diffs are not rendered by default.

265 changes: 265 additions & 0 deletions frames/0029.ppm

Large diffs are not rendered by default.

265 changes: 265 additions & 0 deletions frames/0030.ppm

Large diffs are not rendered by default.

265 changes: 265 additions & 0 deletions frames/0031.ppm

Large diffs are not rendered by default.

265 changes: 265 additions & 0 deletions frames/0032.ppm

Large diffs are not rendered by default.

265 changes: 265 additions & 0 deletions frames/0033.ppm

Large diffs are not rendered by default.

265 changes: 265 additions & 0 deletions frames/0034.ppm

Large diffs are not rendered by default.

265 changes: 265 additions & 0 deletions frames/0035.ppm

Large diffs are not rendered by default.

265 changes: 265 additions & 0 deletions frames/0036.ppm

Large diffs are not rendered by default.

223 changes: 223 additions & 0 deletions frames/0037.ppm

Large diffs are not rendered by default.

151 changes: 151 additions & 0 deletions frames/0038.ppm

Large diffs are not rendered by default.

155 changes: 155 additions & 0 deletions frames/0039.ppm

Large diffs are not rendered by default.

180 changes: 180 additions & 0 deletions frames/0040.ppm

Large diffs are not rendered by default.

212 changes: 212 additions & 0 deletions frames/0041.ppm

Large diffs are not rendered by default.

318 changes: 318 additions & 0 deletions frames/0042.ppm

Large diffs are not rendered by default.

308 changes: 308 additions & 0 deletions frames/0043.ppm

Large diffs are not rendered by default.

285 changes: 285 additions & 0 deletions frames/0044.ppm

Large diffs are not rendered by default.

559 changes: 559 additions & 0 deletions frames/0045.ppm

Large diffs are not rendered by default.

1,003 changes: 1,003 additions & 0 deletions frames/0046.ppm

Large diffs are not rendered by default.

1,148 changes: 1,148 additions & 0 deletions frames/0047.ppm

Large diffs are not rendered by default.

1,088 changes: 1,088 additions & 0 deletions frames/0048.ppm

Large diffs are not rendered by default.

1,219 changes: 1,219 additions & 0 deletions frames/0049.ppm

Large diffs are not rendered by default.

1,461 changes: 1,461 additions & 0 deletions frames/0050.ppm

Large diffs are not rendered by default.

1,795 changes: 1,795 additions & 0 deletions frames/0051.ppm

Large diffs are not rendered by default.

2,068 changes: 2,068 additions & 0 deletions frames/0052.ppm

Large diffs are not rendered by default.

2,253 changes: 2,253 additions & 0 deletions frames/0053.ppm

Large diffs are not rendered by default.

2,209 changes: 2,209 additions & 0 deletions frames/0054.ppm

Large diffs are not rendered by default.

2,196 changes: 2,196 additions & 0 deletions frames/0055.ppm

Large diffs are not rendered by default.

2,375 changes: 2,375 additions & 0 deletions frames/0056.ppm

Large diffs are not rendered by default.

2,143 changes: 2,143 additions & 0 deletions frames/0057.ppm

Large diffs are not rendered by default.

2,144 changes: 2,144 additions & 0 deletions frames/0058.ppm

Large diffs are not rendered by default.

2,148 changes: 2,148 additions & 0 deletions frames/0059.ppm

Large diffs are not rendered by default.

2,148 changes: 2,148 additions & 0 deletions frames/0060.ppm

Large diffs are not rendered by default.

2,148 changes: 2,148 additions & 0 deletions frames/0061.ppm

Large diffs are not rendered by default.

2,143 changes: 2,143 additions & 0 deletions frames/0062.ppm

Large diffs are not rendered by default.

2,143 changes: 2,143 additions & 0 deletions frames/0063.ppm

Large diffs are not rendered by default.

2,143 changes: 2,143 additions & 0 deletions frames/0064.ppm

Large diffs are not rendered by default.

2,143 changes: 2,143 additions & 0 deletions frames/0065.ppm

Large diffs are not rendered by default.

2,143 changes: 2,143 additions & 0 deletions frames/0066.ppm

Large diffs are not rendered by default.

2,143 changes: 2,143 additions & 0 deletions frames/0067.ppm

Large diffs are not rendered by default.

2,143 changes: 2,143 additions & 0 deletions frames/0068.ppm

Large diffs are not rendered by default.

2,143 changes: 2,143 additions & 0 deletions frames/0069.ppm

Large diffs are not rendered by default.

2,143 changes: 2,143 additions & 0 deletions frames/0070.ppm

Large diffs are not rendered by default.

2,143 changes: 2,143 additions & 0 deletions frames/0071.ppm

Large diffs are not rendered by default.

2,143 changes: 2,143 additions & 0 deletions frames/0072.ppm

Large diffs are not rendered by default.

2,143 changes: 2,143 additions & 0 deletions frames/0073.ppm

Large diffs are not rendered by default.

2,143 changes: 2,143 additions & 0 deletions frames/0074.ppm

Large diffs are not rendered by default.

2,143 changes: 2,143 additions & 0 deletions frames/0075.ppm

Large diffs are not rendered by default.

2,143 changes: 2,143 additions & 0 deletions frames/0076.ppm

Large diffs are not rendered by default.

2,143 changes: 2,143 additions & 0 deletions frames/0077.ppm

Large diffs are not rendered by default.

2,143 changes: 2,143 additions & 0 deletions frames/0078.ppm

Large diffs are not rendered by default.

2,143 changes: 2,143 additions & 0 deletions frames/0079.ppm

Large diffs are not rendered by default.

2,143 changes: 2,143 additions & 0 deletions frames/0080.ppm

Large diffs are not rendered by default.

2,143 changes: 2,143 additions & 0 deletions frames/0081.ppm

Large diffs are not rendered by default.

2,143 changes: 2,143 additions & 0 deletions frames/0082.ppm

Large diffs are not rendered by default.

2,143 changes: 2,143 additions & 0 deletions frames/0083.ppm

Large diffs are not rendered by default.

2,143 changes: 2,143 additions & 0 deletions frames/0084.ppm

Large diffs are not rendered by default.

2,143 changes: 2,143 additions & 0 deletions frames/0085.ppm

Large diffs are not rendered by default.

2,143 changes: 2,143 additions & 0 deletions frames/0086.ppm

Large diffs are not rendered by default.

2,149 changes: 2,149 additions & 0 deletions frames/0087.ppm

Large diffs are not rendered by default.

2,186 changes: 2,186 additions & 0 deletions frames/0088.ppm

Large diffs are not rendered by default.

2,196 changes: 2,196 additions & 0 deletions frames/0089.ppm

Large diffs are not rendered by default.

2,237 changes: 2,237 additions & 0 deletions frames/0090.ppm

Large diffs are not rendered by default.

2,207 changes: 2,207 additions & 0 deletions frames/0091.ppm

Large diffs are not rendered by default.

2,236 changes: 2,236 additions & 0 deletions frames/0092.ppm

Large diffs are not rendered by default.

2,219 changes: 2,219 additions & 0 deletions frames/0093.ppm

Large diffs are not rendered by default.

2,219 changes: 2,219 additions & 0 deletions frames/0094.ppm

Large diffs are not rendered by default.

2,219 changes: 2,219 additions & 0 deletions frames/0095.ppm

Large diffs are not rendered by default.

2,219 changes: 2,219 additions & 0 deletions frames/0096.ppm

Large diffs are not rendered by default.

2,213 changes: 2,213 additions & 0 deletions frames/0097.ppm

Large diffs are not rendered by default.

2,213 changes: 2,213 additions & 0 deletions frames/0098.ppm

Large diffs are not rendered by default.

2,213 changes: 2,213 additions & 0 deletions frames/0099.ppm

Large diffs are not rendered by default.

2,213 changes: 2,213 additions & 0 deletions frames/0100.ppm

Large diffs are not rendered by default.

Binary file added reference.vzip
Binary file not shown.
269 changes: 269 additions & 0 deletions serial.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,269 @@
#include <dirent.h>
#include <stdio.h>
#include <assert.h>
#include <stdlib.h>
#include <string.h>
#include <zlib.h>
#include <time.h>
#include <pthread.h>

#define BUFFER_SIZE 1048576 // 1MB
#define THREAD_POOL_LIMIT 19 // 19 + 1 = 20

int cmp(const void *a, const void *b) {
return strcmp(*(char **)a, *(char **)b);
}

pthread_mutex_t fileLock = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
pthread_cond_t cond1 = PTHREAD_COND_INITIALIZER;
int totalCompressFiles = 0, totalFiles = 0;

int totalInput = 0, TotalOut = 0, status = 1;
FILE *f_out;

typedef struct CompressTask {
int threadId;
char *filePath;
} CompressTask ;


CompressTask taskQueue[256];
int taskCount = 0;

void compressFile(CompressTask cTask)
{
// printf("compressing %d\n", cTask.threadId);
unsigned char buffer_in[BUFFER_SIZE];
unsigned char buffer_out[BUFFER_SIZE];

FILE *f_in = fopen(cTask.filePath, "r");
assert(f_in != NULL);
int nbytes = fread(buffer_in, sizeof(unsigned char), BUFFER_SIZE, f_in);
fclose(f_in);

// zip file
z_stream strm;
int ret = deflateInit(&strm, 9);
assert(ret == Z_OK);
strm.avail_in = nbytes;
strm.next_in = buffer_in;
strm.avail_out = BUFFER_SIZE;
strm.next_out = buffer_out;

ret = deflate(&strm, Z_FINISH);
assert(ret == Z_STREAM_END);

pthread_mutex_lock(&fileLock);

while (totalCompressFiles != cTask.threadId) {
pthread_cond_wait(&cond, &fileLock);
}

totalInput += nbytes;

int nbytes_zipped = BUFFER_SIZE - strm.avail_out;
TotalOut += nbytes_zipped;

fwrite(&nbytes_zipped, sizeof(int), 1, f_out);
fwrite(buffer_out, sizeof(unsigned char), nbytes_zipped, f_out);
totalCompressFiles++;
pthread_cond_broadcast(&cond);

pthread_mutex_unlock(&fileLock);
printf("file: %d total: %d\n", totalCompressFiles, totalFiles);
printf("Compression rate: %.2lf%%\n", 100.0 * (totalInput - TotalOut) / totalInput);
if(totalCompressFiles == totalFiles)
{
status = 0;
pthread_cond_signal(&cond1);
}
}

void* startThread(void *args)
{
while (status == 1)
{
CompressTask CTask;

pthread_mutex_lock(&fileLock);

if(totalCompressFiles == totalFiles)
{
status = 0;
break;
}

while (taskCount == 0)
{
pthread_cond_wait(&cond1, &fileLock);
}


CTask = taskQueue[0];
for (int i = 0; i < taskCount - 1; i++)
{
taskQueue[i] = taskQueue[i + 1];
}
taskCount--;
pthread_mutex_unlock(&fileLock);
compressFile(CTask);
}

}

void addTaskToQueue(CompressTask *cTask)
{
pthread_mutex_lock(&fileLock);

taskQueue[taskCount] = *cTask;
taskCount++;

pthread_mutex_unlock(&fileLock);
pthread_cond_signal(&cond1);
}


// void *processPPM(void *ptr) {
// struct threadDTO *trdData = (struct threadDTO *)ptr;
// unsigned char buffer_in[BUFFER_SIZE];
// unsigned char buffer_out[BUFFER_SIZE];

// FILE *f_in = fopen(trdData->filePath, "r");
// assert(f_in != NULL);
// int nbytes = fread(buffer_in, sizeof(unsigned char), BUFFER_SIZE, f_in);
// fclose(f_in);

// // zip file
// z_stream strm;
// int ret = deflateInit(&strm, 9);
// assert(ret == Z_OK);
// strm.avail_in = nbytes;
// strm.next_in = buffer_in;
// strm.avail_out = BUFFER_SIZE;
// strm.next_out = buffer_out;

// ret = deflate(&strm, Z_FINISH);
// assert(ret == Z_STREAM_END);

// pthread_mutex_lock(&file_lock);

// while (next_thread_id != trdData->threadId) {
// pthread_cond_wait(&cond, &file_lock);
// }

// in += nbytes;

// int nbytes_zipped = BUFFER_SIZE - strm.avail_out;
// out += nbytes_zipped;

// fwrite(&nbytes_zipped, sizeof(int), 1, f_out);
// fwrite(buffer_out, sizeof(unsigned char), nbytes_zipped, f_out);
// next_thread_id++;
// pthread_cond_broadcast(&cond);

// pthread_mutex_unlock(&file_lock);

// pthread_exit(NULL);
// }

int main(int argc, char **argv) {
// time computation header
struct timespec start, end;
clock_gettime(CLOCK_MONOTONIC, &start);
// end of time computation header

// do not modify the main function before this point!

assert(argc == 2);

DIR *d;
struct dirent *dir;
char **files = NULL;
int nfiles = 0;

d = opendir(argv[1]);
if (d == NULL) {
printf("An error has occurred\n");
return 0;
}

// create sorted list of PPM files
while ((dir = readdir(d)) != NULL) {
files = realloc(files, (nfiles + 1) * sizeof(char *));
assert(files != NULL);

int len = strlen(dir->d_name);
if (dir->d_name[len - 4] == '.' && dir->d_name[len - 3] == 'p' && dir->d_name[len - 2] == 'p' &&
dir->d_name[len - 1] == 'm') {
files[nfiles] = strdup(dir->d_name);
assert(files[nfiles] != NULL);

nfiles++;
}
}
closedir(d);
qsort(files, nfiles, sizeof(char *), cmp);

// create a single zipped package with all PPM files in lexicographical order

f_out = fopen("video.vzip", "w"); // Open output file once
totalFiles = nfiles;
pthread_t trdPool[THREAD_POOL_LIMIT];

for (int i = 0; i < THREAD_POOL_LIMIT; i++)
{
pthread_create(&trdPool[i], NULL, &startThread, NULL);
}

int threadCounter = 0;
for (int i = 0; i < nfiles; i++) {
int len = strlen(argv[1]) + strlen(files[i]) + 2;
char *full_path = malloc(len * sizeof(char));
assert(full_path != NULL);
strcpy(full_path, argv[1]);
strcat(full_path, "/");
strcat(full_path, files[i]);

struct CompressTask *trdData = malloc(sizeof(struct CompressTask));
trdData->threadId = i;
trdData->filePath = strdup(full_path);

// pthread_create(&trdPool[threadCounter], NULL, processPPM, (void *)trdData);
addTaskToQueue(trdData);
threadCounter++;

// if (threadCounter == THREAD_LIMIT) {
// for (int j = 0; j < THREAD_LIMIT; j++) {
// pthread_join(tid[j], NULL);
// }
// threadCounter = 0;
// }

free(full_path);
}

for (int j = 0; j < THREAD_POOL_LIMIT; j++) {
pthread_join(trdPool[j], NULL);
}

fclose(f_out); // Close output file after all threads finish

printf("Compression rate: %.2lf%%\n", 100.0 * (totalInput - TotalOut) / totalInput);

// release list of files
for (int i = 0; i < nfiles; i++)
free(files[i]);
free(files);

// pthread_mutex_destroy(&lock);

// do not modify the main function after this point!

// time computation footer
clock_gettime(CLOCK_MONOTONIC, &end);
printf("Time: %.2f seconds\n", ((double)end.tv_sec+1.0e-9*end.tv_nsec)-((double)start.tv_sec+1.0e-9*start.tv_nsec));
// end of time computation footer

return 0;
}
Empty file added video.vzip
Empty file.
Binary file added vzip
Binary file not shown.

0 comments on commit 0a54d9d

Please sign in to comment.