Skip to content

Commit 53eb569

Browse files
committed
Made Haar transform multithreaded
1 parent dcf34f3 commit 53eb569

File tree

3 files changed

+43
-12
lines changed

3 files changed

+43
-12
lines changed

fingerprinting.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@
77
#include "spectralimages.h"
88
#include "wav.h"
99

10-
11-
1210
int generate_fingerprint(const char* wav, struct signatures* *fingerprint,
1311
char* *artist, char* *track_title, char* *album_title) {
1412
// Let's make sure we have a wave file we can read
@@ -57,8 +55,10 @@ int generate_fingerprint(const char* wav, struct signatures* *fingerprint,
5755
}
5856

5957
fprintf(stderr, "Got %d spectral images\n", spectral_images->n_images);
58+
fprintf(stderr, "Applying Haar transform to spectral images\n");
6059
apply_Haar_transform(spectral_images);
6160

61+
fprintf(stderr, "Building raw fingerprints\n");
6262
struct rawfingerprints* rawfingerprints = build_raw_fingerprints(spectral_images);
6363
free_spectral_images(spectral_images);
6464
if (rawfingerprints == NULL) {

haar.c

+39-7
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,18 @@
11
#include <math.h>
2+
#include <pthread.h>
23
#include <stdio.h>
34
#include <stdlib.h>
45
#include "haar.h"
56

7+
#define N_THREADS 8
8+
9+
10+
struct haar_transform_job {
11+
struct spectral_image* images;
12+
unsigned int first_image;
13+
unsigned int last_image;
14+
};
15+
616

717
/**
818
* Applies in place the 1-dimension Haar transform.
@@ -34,11 +44,8 @@ static void transform_array(float* data, unsigned int size) {
3444

3545
/**
3646
* Transforms in place a spectral image.
37-
*
38-
* @param data The frame buffer
39-
* @param image_index The index of the image to transform
4047
*/
41-
static void transform_image(struct spectral_image* image) {
48+
void transform_image(struct spectral_image* image) {
4249
// The 2D standard Haar transform consists of applying
4350
// the 1D Haar transform to each row of the image and then
4451
// to each column of the result
@@ -66,8 +73,33 @@ static void transform_image(struct spectral_image* image) {
6673
}
6774

6875

69-
void apply_Haar_transform(struct spectral_images* images) {
70-
for (unsigned int i = 0 ; i < images->n_images ; i++) {
71-
transform_image(&(images->images[i]));
76+
static void* launch_Haar_job(struct haar_transform_job* job) {
77+
for (unsigned int i = job->first_image ; i <= job->last_image ; i++) {
78+
transform_image(&(job->images[i]));
7279
}
80+
81+
return NULL;
7382
}
83+
84+
85+
void apply_Haar_transform(struct spectral_images* spectral_images) {
86+
pthread_t thread[N_THREADS];
87+
struct haar_transform_job haar_job[N_THREADS];
88+
unsigned int images_per_thread = spectral_images->n_images / N_THREADS;
89+
90+
for (unsigned int k = 0 ; k < N_THREADS ; k++) {
91+
unsigned int start = k * images_per_thread;
92+
unsigned int end = (k == N_THREADS - 1)
93+
? spectral_images->n_images - 1
94+
: (k + 1) * images_per_thread - 1;
95+
haar_job[k].images = spectral_images->images;
96+
haar_job[k].first_image = start;
97+
haar_job[k].last_image = end;
98+
99+
pthread_create(&(thread[k]), NULL, (void* (*)(void*))launch_Haar_job, &(haar_job[k]));
100+
}
101+
for (unsigned int k = 0 ; k < N_THREADS ; k++) {
102+
pthread_join(thread[k], NULL);
103+
}
104+
}
105+

haar.h

+2-3
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,10 @@
2222
* getting a smaller image by using the same color for parts that
2323
* are very similar).
2424
*
25-
* The function applies the Haar transform to each spectral image to make
25+
* The function applies the Haar transform to the given spectral images to make
2626
* it suitable for further compression. Since the Haar transform produces
2727
* for each image an output of the same size, this function will modify the
28-
* given data in place. If the number of frames is not divisible by 128,
29-
* the remaining frames at the end are left unmodified.
28+
* given data in place.
3029
*/
3130
void apply_Haar_transform(struct spectral_images* images);
3231

0 commit comments

Comments
 (0)