Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
B
binary_c-python
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Izzard, Robert Dr (Maths & Physics)
binary_c-python
Commits
822d640b
Commit
822d640b
authored
5 years ago
by
David Hendriks
Browse files
Options
Downloads
Patches
Plain Diff
updated logging functions and todo
parent
eb0a6968
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
TODO.org
+2
-1
2 additions, 1 deletion
TODO.org
binary_c_python.h
+0
-2
0 additions, 2 deletions
binary_c_python.h
binaryc_python_utils/logging_functions.py
+116
-8
116 additions, 8 deletions
binaryc_python_utils/logging_functions.py
with
118 additions
and
11 deletions
TODO.org
+
2
−
1
View file @
822d640b
...
@@ -124,7 +124,8 @@ sub autogen_C_logging_code
...
@@ -124,7 +124,8 @@ sub autogen_C_logging_code
*** TODO Make function in python that puts code into c function
*** TODO Make function in python that puts code into c function
*** TODO Make function in python that generates c-function from a list of arguments
*** DONE Make function in python that generates c-function from a list of arguments
CLOSED: [2019-10-29 Tue 23:52]
*** TODO Resolve current issue malloc
*** TODO Resolve current issue malloc
➜ binary_c-python git:(master) ✗ python python_API_test.py
➜ binary_c-python git:(master) ✗ python python_API_test.py
Traceback (most recent call last):
Traceback (most recent call last):
...
...
This diff is collapsed.
Click to expand it.
binary_c_python.h
+
0
−
2
View file @
822d640b
...
@@ -17,8 +17,6 @@ int run_binary_with_log (char * argstring,
...
@@ -17,8 +17,6 @@ int run_binary_with_log (char * argstring,
char
**
outstring
,
char
**
outstring
,
int
*
nbytes
);
int
*
nbytes
);
int
return_arglines
(
char
**
buffer
,
int
return_arglines
(
char
**
buffer
,
int
*
nbytes
);
int
*
nbytes
);
...
...
This diff is collapsed.
Click to expand it.
binaryc_python_utils/logging_functions.py
+
116
−
8
View file @
822d640b
import
textwrap
# Functions for the automatic logging of stuff
# Functions for the automatic logging of stuff
# https://stackoverflow.com/questions/41954269/create-c-function-pointers-structure-in-python
# https://stackabuse.com/enhancing-python-with-custom-c-extensions/ Read
# https://stackoverflow.com/questions/49941617/runtime-generation-and-compilation-of-cython-functions
# https://realpython.com/cpython-source-code-guide/
# https://docs.python.org/3.6/c-api/index.html
# Perl code for autogeneration
# Perl code for autogeneration
# sub autogen_C_logging_code
# sub autogen_C_logging_code
...
@@ -69,19 +76,15 @@ def autogen_C_logging_code(logging_dict):
...
@@ -69,19 +76,15 @@ def autogen_C_logging_code(logging_dict):
# Check if the input is of the correct form
# Check if the input is of the correct form
if
not
type
(
logging_dict
)
==
dict
:
if
not
type
(
logging_dict
)
==
dict
:
print
(
"
Error: please use a dictionary as input
"
)
print
(
"
Error: please use a dictionary as input
"
)
return
None
#
code
=
''
code
=
''
# Loop over dict keys
# Loop over dict keys
for
key
in
logging_dict
:
for
key
in
logging_dict
:
print
(
'
{}
'
.
format
(
key
))
logging_dict_entry
=
logging_dict
[
key
]
logging_dict_entry
=
logging_dict
[
key
]
# Check if item is of correct type:
# Check if item is of correct type:
if
type
(
logging_dict_entry
)
==
list
:
if
type
(
logging_dict_entry
)
==
list
:
print
(
logging_dict_entry
)
# Construct print statement
# Construct print statement
code
+=
'
PRINTF(
"
{}
'
.
format
(
key
)
code
+=
'
PRINTF(
"
{}
'
.
format
(
key
)
...
@@ -92,14 +95,119 @@ def autogen_C_logging_code(logging_dict):
...
@@ -92,14 +95,119 @@ def autogen_C_logging_code(logging_dict):
# Add format keys
# Add format keys
for
param
in
logging_dict_entry
:
for
param
in
logging_dict_entry
:
code
+=
'
,((double)stardata->{})
'
.
format
(
param
)
code
+=
'
,((double)stardata->{})
'
.
format
(
param
)
code
+=
'
);
'
code
+=
'
);
\n
'
else
:
else
:
print
(
'
Error: please use a list for the list of parameters that you want to have logged
'
)
print
(
'
Error: please use a list for the list of parameters that you want to have logged
'
)
code
=
code
.
strip
()
# print("MADE AUTO CODE\n\n{}\n\n{}\n\n{}\n".format('*'*60, repr(code), '*'*60))
return
code
print
(
repr
(
code
))
autogen_C_logging_code
({
'
MY_STELLAR_DATA
'
:
[
'
model.time
'
,
'
star[0].mass
'
],
'
my_sss2
'
:
[
'
model.time
'
,
'
star[1].mass
'
]})
autogen_C_logging_code
(
{
'
MY_STELLAR_DATA
'
:
[
'
model.time
'
,
'
star[0].mass
'
],
'
my_sss2
'
:
[
'
model.time
'
,
'
star[1].mass
'
]
}
)
####################################################################################
# sub binary_c_log_code
# {
# my ($code) = @_;
# return "
# #pragma push_macro(\"MAX\")
# #pragma push_macro(\"MIN\")
# #undef MAX
# #undef MIN
# #include \"binary_c.h\"
# void custom_output_function(SV * x);
# SV * custom_output_function_pointer(void);
# SV * custom_output_function_pointer()
# {
# /*
# * use PTR2UV to convert the function pointer
# * &custom_output_function to an unsigned int,
# * which is then converted to a Perl SV
# */
# return (SV*)newSVuv(PTR2UV(&custom_output_function));
# }
# void custom_output_function(SV * x)
# {
# struct stardata_t * stardata = (struct stardata_t *)x;
# $code;
# }
# #undef MAX
# #undef MIN
# #pragma pop_macro(\"MIN\")
# #pragma pop_macro(\"MAX\")
# ";
# }
# And then to use it via
# $population->set(
# C_logging_code => '
# PRINTF("MY_STELLAR_DATA %g %g %g %g\n",
# stardata->model.time,
# stardata->star[0].mass,
# stardata->model.probability,
# stardata->model.dt);
# '
# );
def
binary_c_log_code
(
code
):
"""
Function to construct the code to construct the custom logging function
"""
custom_logging_function_string
=
"""
#pragma push_macro(
\"
MAX
\"
)
#pragma push_macro(
\"
MIN
\"
)
#undef MAX
#undef MIN
#include
\"
binary_c.h
\"
void custom_output_function(SV * x);
SV * custom_output_function_pointer(void);
SV * custom_output_function_pointer()
{{
/*
* use PTR2UV to convert the function pointer
* &custom_output_function to an unsigned int,
* which is then converted to a Perl SV
*/
return (SV*)newSVuv(PTR2UV(custom_output_function));
}}
void custom_output_function(SV * x)
{{
struct stardata_t * stardata = (struct stardata_t *)x;
{code};
}}
#undef MAX
#undef MIN
#pragma pop_macro(
\"
MIN
\"
)
#pragma pop_macro(
\"
MAX
\"
)
"""
.
format
(
code
=
code
)
print
(
textwrap
.
dedent
(
custom_logging_function_string
))
# return custom_logging_function_string
code
=
autogen_C_logging_code
(
{
'
MY_STELLAR_DATA
'
:
[
'
model.time
'
,
'
star[0].mass
'
],
'
my_sss2
'
:
[
'
model.time
'
,
'
star[1].mass
'
]
}
)
binary_c_log_code
(
code
)
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment