Commit 6598f218 authored by David Hendriks's avatar David Hendriks
Browse files

updated C-code, removed old stuff

parent faa416a8
Loading
Loading
Loading
Loading
+1 −3
Original line number Diff line number Diff line
@@ -6,8 +6,6 @@
 * Include binary_C's API
 */
#include "binary_c.h"
//#include "binary_c_API.h"
//#include "binary_c_API_prototypes.h"

/* Binary_c's python API prototypes */
int run_system(char * argstring,
@@ -42,7 +40,7 @@ int return_version_info(char ** const outstring,
                size_t * const nbytes);

/* =================================================================== */
/* Functions to call other functionality                               */
/* Functions to handle memory                                          */
/* =================================================================== */

long int return_store_memaddr(char ** const buffer,
+70 −131
Original line number Diff line number Diff line
#include <Python.h>
#include "binary_c_python.h"
#include <time.h>
#include <sys/timeb.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <stdint.h>

/*
 * binary_c/PYTHON API interface functions
 *
 * This module will be available as _binary_c_bindings, as a part of the binarycpython package.
 * 
 * The first section contains the functions that will be available 
 * to python as part of the submodule _binary_c_bindings
 * 
 * The second section is composed of the functions that interface with the binary_c API 
 *
 * Written by David Hendriks (davidhendriks93@gmail.com), Robert Izzard (r.izzard@surrey.ac.uk). 
 * Based on initial work of Jeff Andrews
 * Remember: variables must be passed by references
 * (i.e. as pointers).
 *
 * See apitest.py for an example of how to use these functions.
 * See tests/python_API_test.py for an example of how to use these functions.
 *
 * See also
 * Backup reading material for making C-extensions:
 * http://www-h.eng.cam.ac.uk/help/tpl/languages/mixinglanguages.html
 * https://realpython.com/build-python-c-extension-module/
 * https://docs.python.org/3/extending/extending.html
 * https://docs.python.org/3/c-api/arg.html#c.PyArg_ParseTuple
 * https://realpython.com/python-bindings-overview/
 * http://scipy-lectures.org/advanced/interfacing_with_c/interfacing_with_c.html
 * 
 * Open tasks for the Extension:
 * TODO: Put in clear debug statements
 * TODO: properly return stderr
 * TODO: describe all functions with docstrings
 */

// TODO: Put in clear debug statements
/* list of variables used in the Py<>C interface */

/************************************************************/
/*
/************************************************************
 ************************************************************
 ** Section 1: Python module functions and creation of module
 ************************************************************
 ************************************************************/

/************************************************************
 *
 * function prototypes : these are the functions
 * called by PYTHON code, without the trailing underscore.
 */
/************************************************************/
 *
 ************************************************************/

/* Preparing all the functions of the module */
// Docstrings
static char module_docstring[] MAYBE_UNUSED =
    "This module is a python3 wrapper around binary_c";

#ifdef __DEPRECATED
static char create_binary_docstring[] =
    "Allocate memory for a binary";
#endif
static char function_prototype_docstring[] =
    "The prototype for a binary_c python function";
static char new_binary_system_docstring[] =
    "Return an object containing a binary, ready for evolution";

// Evolution function docstrings
static char run_system_docstring[] = 
    "Function to run a system. This is a general function that will be able to handle different kinds of situations: single system run with different settings, population run with different settings, etc. To avoid having too many functions doing slightly different things. \n\nArguments:\n\targstring: argument string for binary_c\n\t(opt) custom_logging_func_memaddr: memory address value for custom logging function. Default = -1 (None)\n\t(opt) store_memaddr: memory adress of the store. Default = -1 (None)\n\t(opt) write_logfile: Boolean (in int form) for whether to enable the writing of the log function. Default = 0\n\t(opt) population: Boolean (in int form) for whether this system is part of a population run. Default = 0.";
@@ -70,12 +86,6 @@ static char free_store_memaddr_docstring[] =
static struct libbinary_c_store_t *store = NULL;

/* Initialize pyobjects */
// Old functions. Can be removed I think
#ifdef __DEPRECATED
static PyObject* binary_c_create_binary(PyObject *self, PyObject *args);
#endif
static PyObject* binary_c_function_prototype(PyObject *self, PyObject *args);
static PyObject* binary_c_new_binary_system(PyObject *self, PyObject *args);

// Evolution function headers
static PyObject* binary_c_run_system(PyObject *self, PyObject *args, PyObject *kwargs);
@@ -96,30 +106,24 @@ static PyObject* binary_c_free_store_memaddr(PyObject *self, PyObject *args);

/* Set the module functions */
static PyMethodDef module_methods[] = {
#ifdef __DEPRECATED
    {"create_binary", 
        binary_c_create_binary, 
        METH_VARARGS, 
        create_binary_docstring
    },
#endif
    {"function_prototype", binary_c_function_prototype, METH_VARARGS, function_prototype_docstring},
    {"new_system", binary_c_new_binary_system, METH_VARARGS, new_binary_system_docstring},

    {"run_system", (PyCFunction)binary_c_run_system, METH_VARARGS|METH_KEYWORDS, run_system_docstring}, 
    // Wierdly, this casting to a PyCFunction, which usually takes only 2 args, now works when giving keywords. See https://stackoverflow.com/q/10264080
    {"run_system", (PyCFunction)binary_c_run_system, METH_VARARGS|METH_KEYWORDS, run_system_docstring}, 

    //
    {"return_arglines", binary_c_return_arglines, METH_VARARGS, return_arglines_docstring},
    {"return_help", binary_c_return_help_info, METH_VARARGS, return_help_info_docstring},
    {"return_help_all", binary_c_return_help_all_info, METH_VARARGS, return_help_all_info_docstring},
    {"return_version_info", binary_c_return_version_info, METH_VARARGS, return_version_info_docstring},

    //
    {"return_store_memaddr", binary_c_return_store_memaddr, METH_VARARGS, return_store_memaddr_docstring},
    {"return_persistent_data_memaddr", binary_c_return_persistent_data_memaddr, METH_NOARGS, return_persistent_data_memaddr_docstring},

    //
    {"free_persistent_data_memaddr_and_return_json_output", binary_c_free_persistent_data_memaddr_and_return_json_output, METH_VARARGS, free_persistent_data_memaddr_and_return_json_output_docstring},
    {"free_store_memaddr", binary_c_free_store_memaddr, METH_VARARGS, free_store_memaddr_docstring},

    //
    {NULL, NULL, 0, NULL}
};

@@ -133,7 +137,7 @@ static struct PyModuleDef Py__binary_c_bindings =
{
    PyModuleDef_HEAD_INIT,
    "_binary_c_bindings", /* name of module */
    "TODO",          /* module documentation, may be NULL */
    "Module to interface the Binary_c API with python.",          /* module documentation, may be NULL */
    -1,          /* size of per-interpreter state of the module, or -1 if the module keeps state in global variables. */
    module_methods
};
@@ -147,73 +151,6 @@ PyMODINIT_FUNC PyInit__binary_c_bindings(void)
/* Some function that we started out with. Unused now.                            */
/* ============================================================================== */

#ifdef __DEPRECATED
static PyObject* binary_c_create_binary(PyObject *self, PyObject *args)
{

    double var1, var2;
    char * empty_str = "";
    int i;
    const int N = 1;

    /* Parse the input tuple */
    if(!PyArg_ParseTuple(args, "dd", &var1, &var2))
        return NULL;


    /* Binary structures */
    struct libbinary_c_stardata_t *stardata[N];
    struct libbinary_c_store_t *store = NULL;

    /* Allocate memory for binaries */
    for(i=0;i<N;i++){
        stardata[i] = NULL;
        binary_c_new_system(&stardata[i], NULL, NULL, &store, NULL, &empty_str, -1);
    }

    /* Return the evolved binary */
    PyObject *ret = Py_BuildValue("");

    return ret;
}
#endif //__DEPRECATED


static PyObject* binary_c_new_binary_system(PyObject *self, PyObject *args)
{
    /* Binary structures */
    struct libbinary_c_stardata_t *stardata;

    /* Allocate memory for binaries */
    char * empty_str = "";
    stardata = NULL;
    binary_c_new_system(&stardata, NULL, NULL, &store, NULL, &empty_str, -1);
    
    /* Return an object containing the stardata */
    PyObject *ret = Py_BuildValue("");
    return ret;
}

static PyObject* binary_c_function_prototype(PyObject *self, PyObject *args)
{

    // This function is an very bare example of how a function would look like.

    double var1, var2;

    /* Parse the input tuple */
    if(!PyArg_ParseTuple(args, "dd", &var1, &var2))
    {
        return NULL;
    }
    else
    {
        /* Return the evolved binary */
        PyObject *ret = Py_BuildValue("");
        return ret;
    }
}

/*
    Below are the real functions:
    binary_c_run_population
@@ -280,7 +217,6 @@ static PyObject* binary_c_run_system(PyObject *self, PyObject *args, PyObject *k
    Safe_free(buffer);
    Safe_free(error_buffer);

    // TODO: fix that the return_error_string is returned.
    return return_string;
}

@@ -419,7 +355,6 @@ static PyObject* binary_c_return_store_memaddr(PyObject *self, PyObject *args)
    PyObject * return_error_string MAYBE_UNUSED = Py_BuildValue("s", error_buffer);

    PyObject * store_memaddr = Py_BuildValue("l", out);
    // printf("store_memaddr: %ld\n", out);

    if(error_buffer != NULL && strlen(error_buffer)>0)
    {
@@ -511,7 +446,6 @@ static PyObject* binary_c_free_store_memaddr(PyObject *self, PyObject *args)
    /* Parse the input tuple */
    if(!PyArg_ParseTuple(args, "l", &store_memaddr))
    {
        // printf("Error: got a bad input\n");
        fprintf(stderr,
                "Error (in function: binary_c_free_store_memaddr): Got a bad input\n");
        return NULL;
@@ -532,7 +466,6 @@ static PyObject* binary_c_free_store_memaddr(PyObject *self, PyObject *args)

    if(error_buffer != NULL && strlen(error_buffer)>0)
    {
        // printf("Error (in function: binary_c_free_store_memaddr): %s", error_buffer);
        fprintf(stderr,
                "Error (in function: binary_c_free_store_memaddr): %s\n",
                error_buffer);
@@ -541,38 +474,40 @@ static PyObject* binary_c_free_store_memaddr(PyObject *self, PyObject *args)
    Safe_free(buffer);
    Safe_free(error_buffer);

    return Py_BuildValue("");
    Py_RETURN_NONE;
}

/////////////////////////////////////////////////////////////////////////////////////////////////////////


#include "binary_c_python.h"
#include <time.h>
#include <sys/timeb.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <stdint.h>
/************************************************************
 ************************************************************
 ** Section 2: binary_c interfacing functions
 **
 ** These functions call the binary_c API 
 ************************************************************
 ************************************************************/

/* Binary_c python API 
 * Set of c-functions that interface with the binary_c api.
 * These functions are called by python, first through binary_c_python.c, 
 * and there further instructions are given on how to interface
 * These functions are called by python, through the functions defined above. 
 * 
 * Contains several functions:
 * // evolution
 * // evolution functions:
 * run_system

 * // utility
 * // utility functions:
 * return_arglines
 * return_help_info
 * return_help_all_info
 * return_version_info

 * // other
 * create_store
 * // memory allocating functions:
 * return_store_memaddr
 * free_persistent_data_memaddr_and_return_json_output
 * 
 * // Memory freeing functions:
 * free_store_memaddr 
 * free_persistent_data_memaddr_and_return_json_output
 */

// #define _CAPTURE
@@ -899,7 +834,7 @@ int return_version_info(char ** const buffer,
}

/* =================================================================== */
/* Functions set up memoery adresses                                   */
/* Functions to set up memory                                          */
/* =================================================================== */

long int return_store_memaddr(char ** const buffer,
@@ -921,8 +856,8 @@ long int return_store_memaddr(char ** const buffer,
    );

    /* output to strings */
    // stardata->preferences->internal_buffering = INTERNAL_BUFFERING_STORE;
    // stardata->preferences->batchmode = BATCHMODE_LIBRARY;
    stardata->preferences->internal_buffering = INTERNAL_BUFFERING_STORE;
    stardata->preferences->batchmode = BATCHMODE_LIBRARY;

    /* get buffer pointer */
    binary_c_buffer_info(stardata, buffer, nbytes);
@@ -930,7 +865,7 @@ long int return_store_memaddr(char ** const buffer,
    /* get error buffer pointer */
    binary_c_error_buffer(stardata, error_buffer);
        
    /* free stardata (except the buffer) */
    /* free stardata (except the buffer and the store) */
    binary_c_free_memory(&stardata, // Stardata
        TRUE,                       // free_preferences
        TRUE,                       // free_stardata
@@ -978,7 +913,7 @@ long int return_persistent_data_memaddr(char ** const buffer,
    uintptr_t persistent_data_memaddr_int = (uintptr_t)stardata->persistent_data; // C Version converting ptr to int
    debug_printf("persistent_data is at address: %p persistent_data_memaddr_int: %ld\n", (void*)&stardata->persistent_data, persistent_data_memaddr_int);
    
    /* free stardata (except the buffer) */
    /* free stardata (except the buffer and the persistent data) */
    binary_c_free_memory(&stardata, // Stardata
        TRUE,                       // free_preferences
        TRUE,                       // free_stardata
@@ -992,7 +927,7 @@ long int return_persistent_data_memaddr(char ** const buffer,
}

/* =================================================================== */
/* Functions free memory                                               */
/* Functions to free memory                                            */
/* =================================================================== */

int free_persistent_data_memaddr_and_return_json_output(long int persistent_data_memaddr,
@@ -1029,6 +964,9 @@ int free_persistent_data_memaddr_and_return_json_output(long int persistent_data
                        -1                  // argc
    );

    /* Set the model hash (usually done internally but we're not evolving a system here */
    stardata->model.ensemble_hash = stardata->persistent_data->ensemble_hash;

    /* output to strings */
    stardata->preferences->internal_buffering = INTERNAL_BUFFERING_STORE;
    stardata->preferences->batchmode = BATCHMODE_LIBRARY;
@@ -1048,7 +986,7 @@ int free_persistent_data_memaddr_and_return_json_output(long int persistent_data
        TRUE,                       // free_stardata
        TRUE,                       // free_store
        FALSE,                      // free_raw_buffer
        FALSE                       // free_persistent
        FALSE                       // free_persistent. It already 
    );

    return 0;
@@ -1087,7 +1025,8 @@ int free_store_memaddr(long int store_memaddr,
                        -1                  // argc
    );

    printf("freed store memaddr\n");
    debug_printf("freed store memaddr\n");

    /* output to strings */
    stardata->preferences->internal_buffering = INTERNAL_BUFFERING_STORE;
    stardata->preferences->batchmode = BATCHMODE_LIBRARY;

src/binary_c_python_api.c

deleted100644 → 0
+0 −563

File deleted.

Preview size limit exceeded, changes collapsed.