Saturday, December 27, 2008

Embedding Python

I've finally written my first embedded Python program! The following is basically a "hello world" program that demonstrates the basics of writing a C program that interacts with Python. It starts the embedded Python environment, parses some input (which is stored in the __main__ namespace) and prints out that input directly from C.

#include <Python.h>
#include <stdio.h>
#include <stdlib.h>

int main(int argc, char *argv[])
{
PyObject *globals, *list, *name, *item, *mp;
Py_ssize_t i, n;

Py_Initialize();
PyRun_SimpleString("a = 'some text'; b = 123.456");
mp = PyImport_AddModule("__main__");
list = PyObject_Dir(mp);
n = PyList_Size(list);
printf("n=%d\n", n);
for(i = 0; i < n; i++) {
name = PyList_GetItem(list, i);
item = PyObject_GetAttr(mp, name);
if(PyString_Check(item))
printf("list[%d] (string): '%s' = '%s'\n", i, PyString_AsString(name), PyString_AsString(item));
else if(PyFloat_Check(item))
printf("list[%d] (float): '%s' = %g\n", i, PyString_AsString(name), PyFloat_AsDouble(item));
}
Py_Finalize();

return 0;
}


To compile this program, you'll need to do something like
gcc embed_python.c -I/usr/include/python2.5 -lpython2.5

Thursday, December 18, 2008

Modern physics course at Stanford



Apparently Stanford University has published a series of modern physics lecture videos (taught by Prof. Leonard Susskind) on Youtube. Although this is a continuing studies course, it is definitely graduate level, but taught in a very friendly manner -- a very good supplement for even graduate students of physics.

The rest of the videos are available via this link.

Monday, December 15, 2008

SI vs. Gaussian units

This is a major source of confusion for many physics students, including me. The following link is an article that explains the differences quite clearly, citing some of the historical reasons for having two popular unit systems in use at the same time.

S.I. versus c.g.s. Units by Jason Harlow

Wednesday, December 3, 2008

Multi-threaded programming

This is something I've been curious about for a long time but haven't really had the chance to look into. Thanks to this tutorial at LLNL, I've discovered that it's actually quite easy to do simple multi-threading with POSIX threads -- all you really need are basic C programming skills and a basic concept of how processes are managed by a POSIX operating system. The API is easy to learn, and there is MUCH less overhead compared to multi-host parallel programming. On SMP machines, it is probably much more powerful than using a full parallel enviornment like MPI.