Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

import files from svn

  • Loading branch information...
commit 0ea908928e37be473dfc2c2c9d438139ce658952 1 parent f6e1668
@llaisdy authored
View
8 compile.sh
@@ -0,0 +1,8 @@
+#! /bin/bash
+
+gcc -Wall \
+ -I/usr/local/lib/erlang/usr/include \
+ -fPIC \
+ -o simple_nif.so \
+ -shared \
+ simple_nif.c simple.c
View
10 compile_mac.sh
@@ -0,0 +1,10 @@
+#! /bin/bash
+
+gcc \
+ -Wall \
+ -arch i386 -arch x86_64 \
+ -I/usr/local/lib/erlang/usr/include \
+ -fPIC \
+ -bundle -flat_namespace -undefined suppress \
+ -o simple_nif.so \
+ simple_nif.c simple.c
View
50 passing_list/simple_nif.c
@@ -0,0 +1,50 @@
+#include "erl_nif.h"
+#include "simple.h"
+
+static ERL_NIF_TERM get_params_nif(ErlNifEnv* env,
+ int argc,
+ ERL_NIF_TERM argv[])
+{
+ ERL_NIF_TERM list = argv[0];
+ unsigned int args_size;
+ ERL_NIF_TERM head, tail;
+ int i = 0;
+ int j;
+
+ if (!enif_get_list_length(env, list, &args_size)) {
+ return enif_make_badarg(env);
+ }
+
+ int args[args_size];
+
+ while(enif_get_list_cell(env, list, &head, &tail)) {
+ if(!enif_get_int(env, head, &args[i])) {
+ return enif_make_badarg(env);
+ }
+ i++;
+ list = tail;
+ }
+
+ params_t params = get_params(args, args_size);
+
+ ERL_NIF_TERM record_name = enif_make_atom(env, "params\0");
+ ERL_NIF_TERM sum = enif_make_int(env, params.sum);
+ ERL_NIF_TERM mean = enif_make_double(env, params.mean);
+
+ ERL_NIF_TERM ent_quartiles_array[3];
+ for (j = 0; j < 3; j++) {
+ ent_quartiles_array[j] = enif_make_double(env, params.quartiles[j]);
+ }
+ ERL_NIF_TERM quartiles = enif_make_tuple_from_array(env, ent_quartiles_array, 3);
+
+ return enif_make_tuple4(env, record_name, sum, mean, quartiles);
+}
+
+static ErlNifFunc nif_funcs[] = {
+ {"get_params", 1, get_params_nif} // see note below
+};
+// eoia:326 has ... &get_params_nif
+// doesn't seem to make any difference
+
+ERL_NIF_INIT(simple, nif_funcs, NULL, NULL, NULL, NULL)
+
View
29 passing_list/simple_test.erl
@@ -0,0 +1,29 @@
+-module(simple_test).
+-export([test/2]).
+-export([get_random_input/2]).
+
+-record(params, {sum, mean, quartiles}).
+
+test(N, Max) ->
+ Input = get_random_input(N, Max),
+ io:format("Input: ~p~n~n", [Input]),
+ WindowList = getWindowList(Input, 10, 5),
+ io:format("WindowList: ~p~n~n", [WindowList]),
+ ParamsList = lists:map(fun(W) -> simple:get_params(W) end, WindowList),
+ ParamsList.
+
+get_random_input(0, _Max) ->
+ [];
+get_random_input(N, Max) ->
+ [random:uniform(Max) | get_random_input(N-1,Max)].
+
+getWindowList(DataList, Size, _Offset) when length(DataList) < Size ->
+ [DataList ++ makeList(0, Size - length(DataList))];
+
+getWindowList(DataList, Size, Offset) ->
+ [lists:sublist(DataList, Size) | getWindowList(lists:nthtail(Offset, DataList), Size, Offset)].
+
+makeList(_Item, 0) ->
+ [];
+makeList(Item, N) ->
+ [Item | makeList(Item, N-1)].
View
44 passing_tuple/simple_nif.c
@@ -0,0 +1,44 @@
+#include "erl_nif.h"
+#include "simple.h"
+
+static ERL_NIF_TERM get_params_nif(ErlNifEnv* env,
+ int argc,
+ ERL_NIF_TERM argv[])
+{
+ int args_size;
+ int i,j;
+ const ERL_NIF_TERM* ent_args;
+
+ if (!enif_get_tuple(env, argv[0], &args_size, &ent_args)) {
+ return enif_make_badarg(env);
+ }
+ int args[args_size];
+
+ for(i = 0; i < args_size; i++){
+ enif_get_int(env, ent_args[i], &args[i]);
+ }
+
+ params_t params = get_params(args, args_size);
+
+
+ ERL_NIF_TERM record_name = enif_make_atom(env, "params\0");
+ ERL_NIF_TERM sum = enif_make_int(env, params.sum);
+ ERL_NIF_TERM mean = enif_make_double(env, params.mean);
+
+ ERL_NIF_TERM ent_quartiles_array[3];
+ for (j = 0; j < 3; j++) {
+ ent_quartiles_array[j] = enif_make_double(env, params.quartiles[j]);
+ }
+ ERL_NIF_TERM quartiles = enif_make_tuple_from_array(env, ent_quartiles_array, 3);
+
+ return enif_make_tuple4(env, record_name, sum, mean, quartiles);
+}
+
+static ErlNifFunc nif_funcs[] = {
+ {"get_params", 1, get_params_nif} // see note below
+};
+// eoia:326 has ... &get_params_nif
+// doesn't seem to make any difference
+
+ERL_NIF_INIT(simple, nif_funcs, NULL, NULL, NULL, NULL)
+
View
33 passing_tuple/simple_test.erl
@@ -0,0 +1,33 @@
+-module(simple_test).
+-export([test/2]).
+-export([get_random_input/2]).
+
+-record(params, {sum, mean, quartiles}).
+
+test(N, Max) ->
+ Input = get_random_input(N, Max),
+ io:format("Input: ~p~n~n", [Input]),
+ WindowList = getWindowList(Input, 10, 5),
+ io:format("WindowList: ~p~n~n", [WindowList]),
+ ParamsList = lists:map(fun(W) ->
+ Tup = list_to_tuple(W),
+ simple:get_params(Tup)
+ end,
+ WindowList),
+ ParamsList.
+
+get_random_input(0, _Max) ->
+ [];
+get_random_input(N, Max) ->
+ [random:uniform(Max) | get_random_input(N-1,Max)].
+
+getWindowList(DataList, Size, _Offset) when length(DataList) < Size ->
+ [DataList ++ makeList(0, Size - length(DataList))];
+
+getWindowList(DataList, Size, Offset) ->
+ [lists:sublist(DataList, Size) | getWindowList(lists:nthtail(Offset, DataList), Size, Offset)].
+
+makeList(_Item, 0) ->
+ [];
+makeList(Item, N) ->
+ [Item | makeList(Item, N-1)].
View
91 simple.c
@@ -0,0 +1,91 @@
+#include <stdio.h>
+#include "simple.h"
+
+double sum(const int *args, const int size){
+ double result = 0;
+ int i;
+ for (i = 0; i < size; i++){
+ result += args[i];
+ }
+ return result;
+}
+
+double mean(const int *args, const int size){
+ double s = sum(args, size);
+ double mean = s/size;
+ return mean;
+}
+
+// based on:
+// http://en.wikiversity.org/wiki/C_source_code_to_find_the_median_and_mean
+double median(const int *args, const int size){
+ int tmpArgs[size];
+ int temp;
+ int i, j;
+ for(i=0; i<size-1; i++) {
+ tmpArgs[i] = args[i];
+ }
+ // sort array tmpArgs
+ for(i=0; i<size-1; i++) {
+ for(j=i+1; j<size; j++) {
+ if(tmpArgs[j] < tmpArgs[i]) {
+ temp = tmpArgs[i];
+ tmpArgs[i] = tmpArgs[j];
+ tmpArgs[j] = temp;
+ }
+ }
+ }
+
+ if(size%2==0) {
+ return((args[size/2] + args[size/2 - 1]) / 2.0);
+ } else {
+ return args[size/2];
+ }
+}
+
+void get_quartiles(double* quartiles, const int *args, const int size){
+ int tmpArgs[size];
+ int temp;
+ int i, j;
+ int qidx = (size-1)/4;
+ for(i=0; i<size; i++) {
+ tmpArgs[i] = args[i];
+ }
+ // sort array tmpArgs
+ for(i=0; i<size-1; i++) {
+ for(j=i+1; j<size; j++) {
+ if(tmpArgs[j] < tmpArgs[i]) {
+ temp = tmpArgs[i];
+ tmpArgs[i] = tmpArgs[j];
+ tmpArgs[j] = temp;
+ }
+ }
+ }
+ if (size%2==0) {
+ quartiles[1] = (tmpArgs[size/2 - 1] + tmpArgs[size/2]) / 2.0;
+ if ((size)%4==0) {
+ quartiles[0] = (tmpArgs[qidx - 1] + tmpArgs[qidx]) / 2.0;
+ quartiles[2] = (tmpArgs[size - 1 - qidx] + tmpArgs[size - qidx]) / 2.0;
+ } else {
+ quartiles[0] = tmpArgs[qidx];
+ quartiles[2] = tmpArgs[size - 1 - qidx];
+ }
+ } else {
+ quartiles[1] = tmpArgs[size/2];
+ if ((size-1)%4==0) {
+ quartiles[0] = (tmpArgs[qidx - 1] + tmpArgs[qidx]) / 2.0;
+ quartiles[2] = (tmpArgs[size - 1 - qidx] + tmpArgs[size - qidx]) / 2.0;
+ } else {
+ quartiles[0] = tmpArgs[qidx];
+ quartiles[2] = tmpArgs[size - 1 - qidx];
+ }
+ }
+}
+
+params_t get_params(const int *args, const int size) {
+ params_t params;
+ params.sum = sum(args, size);
+ params.mean = mean(args, size);
+ get_quartiles(params.quartiles, args, size);
+ return params;
+}
View
11 simple.erl
@@ -0,0 +1,11 @@
+-module(simple).
+-export([get_params/1, init/0]).
+-on_load(init/0).
+
+-record(params, {sum, mean, quartiles}).
+
+init() ->
+ ok = erlang:load_nif("./simple_nif", 0).
+
+get_params(_X) ->
+ exit(nif_library_not_loaded).
View
13 simple.h
@@ -0,0 +1,13 @@
+double sum(const int *args, const int size);
+double mean(const int *args, const int size);
+double median(const int *args, const int size);
+void get_quartiles(double* quartiles, const int *args, const int size);
+
+typedef struct {
+ int sum;
+ double mean;
+ double quartiles[3];
+} params_t;
+
+params_t get_params(const int *args, const int size);
+
View
21 simple_main.c
@@ -0,0 +1,21 @@
+#include <stdio.h>
+#include "simple.h"
+
+int main () {
+ int args_size = 9;
+ //int args[] = { 1, 2, 3, 4, 5 };
+
+ int args[args_size];
+ int i;
+ for (i=0; i<args_size; i++) {
+ scanf("%i", &args[i]);
+ }
+
+ params_t params = get_params(args, args_size);
+
+ printf("%i %f\n", params.sum, params.mean);
+ printf("%f %f %f\n",
+ params.quartiles[0], params.quartiles[1], params.quartiles[2]);
+
+ return 0;
+}
Please sign in to comment.
Something went wrong with that request. Please try again.