Skip to content

Commit

Permalink
Well, I'm flushing out a module API, so we can then flush out a
Browse files Browse the repository at this point in the history
protocol.
  • Loading branch information
Nikhil Samith Bysani committed Jan 12, 2009
1 parent db722a2 commit f55c807
Show file tree
Hide file tree
Showing 2 changed files with 107 additions and 4 deletions.
46 changes: 46 additions & 0 deletions src/base/bang-module-api.h
Expand Up @@ -9,6 +9,29 @@

#ifndef __BANG_MODULE_API_H
#define __BANG_MODULE_API_H

typedef struct {
void *data;
unsigned int length;
/**
* The id of the peer who gave you this job.
*/
int authority;
} BANG_job;

/**
* Callbacks that the module _has_ to provide
* to know about events.
*
* Returned from the module init function.
*/
typedef struct {
void (*outgoing_job) (int);
void (*incoming_job) (BANG_job);
void (*peer_added) (int);
void (*peer_removed) (int);
} BANG_callbacks;

/**
* \param The message you want to send to all peers.
*
Expand All @@ -35,6 +58,27 @@ int BANG_number_of_active_peers();
*/
int BANG_get_my_id();

/**
* \brief Tells all your peers that peer id has a job waiting.
*/
void BANG_assert_authority(int id);

/**
* \param id The peer to get a job from.
* \param blocking If true, the callback will be used instead.
*
* \return A job for you to act upon.
*
* \brief If non-null, there is a job waiting for the module.
* Else, an error or non blocking happened.
*
*/
BANG_job* BANG_request_job(int id, char blocking);

int BANG_finished_request(BANG_job job);

void BANG_send_job(int id, BANG_job job);

/**
* The api for the modules to use.
*/
Expand All @@ -46,5 +90,7 @@ typedef struct {
void (*BANG_get_me_peers) ();
int (*BANG_number_of_active_peers) ();
int (*BANG_get_my_id) ();
void (*BANG_assert_authority) (int id);
BANG_job* (*BANG_request_job) (int id, char blocking);
} BANG_api;
#endif
65 changes: 61 additions & 4 deletions src/modules/matrix-mult-module.c
Expand Up @@ -10,21 +10,78 @@
#include"../base/bang-module-api.h"
#include<gtk/gtk.h>

static BANG_api api;

const char BANG_module_name[] = "matrix multiplication";
const unsigned char BANG_module_version[] = {0,0,1};


static BANG_api api;

typedef struct {
double *row;
double *column;
unsigned int length;
} matrix_multi_job;

static double multiply_row(double *column, double *row, unsigned int length);

static void job_callback(BANG_job job);

static matrix_multi_job extract_multi_job(BANG_job job);

static double multiply_row_using_job(matrix_multi_job job);

static void job_callback(BANG_job job) {
double result = multiply_row_using_job(extract_multi_job(job));
free(job.data);
job.data = &result;
/* This is pretty flimsy, but I'm pretty sure that double doesn't
* change size across platforms. */
job.length = sizeof(double);
BANG_finished_request(job);
}

static double multiply_row(double *row, double *column, unsigned int length) {
unsigned int i = 0;
double sum = 0;
for (; i < length; ++i) {
sum += row[i] * column[i];
}
return sum;
}

static double multiply_row_using_job(matrix_multi_job job) {
return multiply_row(job.row,job.column,job.length);
}

static matrix_multi_job extract_multi_job(BANG_job job) {
matrix_multi_job mjob;
/* job.length should always be 2n because this module creates it */
mjob.length = job.length / 2;
mjob.row = (double*) job.data;
mjob.column = ((double*) job.data) + mjob.length;

return mjob;
}

void GUI_init(GtkWidget **page, GtkWidget **page_label) {
*page_label = gtk_label_new("Module of Matrix Multiplication");
*page = gtk_vbox_new(FALSE,0);
gtk_widget_show(*page_label);
gtk_widget_show(*page);
}

int BANG_module_init(BANG_api get_api) {

BANG_callbacks BANG_module_init(BANG_api get_api) {
api = get_api;
return 0;

/*TODO: Make callbacks and finish this */
BANG_callbacks callbacks;
callbacks.incoming_job = &job_callback;
callbacks.outgoing_job = NULL;
callbacks.peer_added = NULL;
callbacks.peer_removed = NULL;

return callbacks;
}

void BANG_module_run() {
Expand Down

0 comments on commit f55c807

Please sign in to comment.