The following C source code multiples two vectors together and returns the resulting
vector.
/*
////////////////////////////////////////////////////////////////////
// File : mcf.c
// Date : 1/8/96
// Copyright : Altair Software Development, Inc. 1996
//
// The multiplication C-only filter
//
////////////////////////////////////////////////////////////////////
*/
#include <stdio.h>
#include <stdlib.h>
#include "ipclib/base/altair_ipc.h"
int main(argc, argv)
int argc;
char *argv[];
{
AltairIpcVectorList vec_list;
double *new_vec_list;
double *first_vec_list;
double *second_vec_list;
char msg_buff[1024];
int cnt;
/*
* You must do this before anything else...
*/
if (AltairIpcParseCommandLine(argc, argv) != ALTAIR_IPC_SUCCESS)
exit(1);
AltairIpcPutMessage("Child [1]");
if (AltairIpcGetVectorList(&vec_list) != ALTAIR_IPC_SUCCESS)
return -1;
AltairIpcPutMessage("Child [2]");
if (vec_list.num_vectors != 2)
{
sprintf(msg_buff, "Expecting only two vectors, got %d",
vec_list.num_vectors);
AltairIpcPutError(msg_buff);
return -1;
}
/*
* To make this code more readable I'm going to use less
* pointer indirection by using the following variables.
*/
first_vec_list = vec_list.vec_val[0];
second_vec_list = vec_list.vec_val[1];
/*
* Multiply them
*/
if (vec_list.vec_cnt[0] == vec_list.vec_cnt[1])
{
AltairIpcPutMessage("Multiplying input vectors...");
new_vec_list = malloc(vec_list.vec_cnt[0] * sizeof(double));
if ( ! new_vec_list)
{
AltairIpcPutError("'malloc(new_vec_list...' failed");
}
for (cnt = 0; cnt < vec_list.vec_cnt[0]; ++cnt)
{
new_vec_list[cnt] = first_vec_list[cnt] * second_vec_list[cnt];
if ( (cnt % 5) == 0)
{
sprintf(msg_buff, "[%d] val = %lf", cnt, new_vec_list[cnt]);
AltairIpcPutMessage(msg_buff);
}
}
AltairIpcPutVector(vec_list.vec_cnt[0], new_vec_list);
free(new_vec_list);
}
else
{
AltairIpcPutMessage("Returning first vector...");
AltairIpcPutVector(vec_list.vec_cnt[0], first_vec_list);
}
/*
* Be a good boy and free my memory (even though the application is
* about to end).
*/
if (first_vec_list)
free(first_vec_list);
if (second_vec_list)
free(second_vec_list);
return 0;
}