forked from daniel-thom/HPC
-
Notifications
You must be signed in to change notification settings - Fork 0
/
spam.c
154 lines (129 loc) · 3.3 KB
/
spam.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
/*
* from Python
* from spam import *
* findcore() : return the core on which a process is running
* forcecore(c) : move the calling process to given core
* p_to_c(pid,c) : move process pid to core c, not currently working on Eagle
*/
/**********
To copile for use with a C program compile with the line:
gcc -DCONLY -c spam.c
EXAMPLE from C:
#include <stdio.h>
void FORCECORE (int *core);
void FINDCORE (int *core);
void P_TO_C (int * pid ,int *core);
main() {
int i,j;
FINDCORE(&i);
printf("on core %d\n",i);
j=(i+1) % 4;
printf("requesting core %d\n",j);
FORCECORE(&j);
FINDCORE(&i);
printf("on core %d\n",i);
}
**********/
#ifndef CONLY
#define PY_SSIZE_T_CLEAN
#include <Python.h>
#else
#define _GNU_SOURCE
#endif
#include <inttypes.h>
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <sched.h>
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#include <assert.h>
pid_t getpid(void);
void FORCECORE (int *core) {
int bonk;
cpu_set_t set;
bonk=*core;
bonk=abs(bonk) ;
CPU_ZERO(&set); // clear cpu mask
CPU_SET(bonk, &set); // set cpu 0
if (*core < 0 ){
sched_setaffinity(0, sizeof(cpu_set_t), &set);
}else{
sched_setaffinity(getpid(), sizeof(cpu_set_t), &set);
}
}
void FINDCORE (int *ic)
{
ic[0] = sched_getcpu();
}
void P_TO_C (int * pid ,int *core) {
cpu_set_t set;
pid_t apid;
int i;
apid=(pid_t)*pid;
CPU_ZERO(&set); // clear cpu mask
int bonk;
bonk=*core;
CPU_SET(bonk, &set); // set cpu 0
i=sched_setaffinity(apid, sizeof(cpu_set_t), &set);
printf("%d\n",i);
printf("not working %ld %d call from process\n",(long)apid,bonk);
}
#ifndef CONLY
static PyObject * findcore(PyObject *self, PyObject *args)
{
size_t gotit;
int core;
FINDCORE(&core);
gotit=(long)core;
return PyLong_FromLong((long)gotit);
}
static PyObject * forcecore(PyObject *self, PyObject *args)
{
size_t gotit;
long lastbase;
int core;
if (!PyArg_ParseTuple(args, "l", &lastbase))
return NULL;
core=(int)lastbase;
FORCECORE(&core);
gotit=0;
return PyLong_FromLong((long)gotit);
}
static PyObject * p_to_c(PyObject *self, PyObject *args)
{
size_t id,p;
size_t gotit;
int pid,core;
if (!PyArg_ParseTuple(args, "ll", &id,&p))
return NULL;
pid=(int)id;
core=(int)p;
P_TO_C(&pid,&core);
gotit=0;
//printf("gotit= %ld\n",(long)gotit);
return PyLong_FromLong((long)gotit);
}
// Py_INCREF(Py_None);
// return Py_None;
static PyMethodDef SpamMethods[] = {
{"findcore", findcore, METH_VARARGS,"Find the core on which calling task is running"},
{"forcecore", forcecore, METH_VARARGS,"Force calling task to a core"},
{"p_to_c", p_to_c, METH_VARARGS,"Force task to core"},
{NULL, NULL, 0, NULL} /* Sentinel */
};
static struct PyModuleDef spammodule = {
PyModuleDef_HEAD_INIT,
"spam", /* name of module */
NULL, /* module documentation, may be NULL */
-1, /* size of per-interpreter state of the module,
or -1 if the module keeps state in global variables. */
SpamMethods
};
PyMODINIT_FUNC
PyInit_spam(void)
{
return PyModule_Create(&spammodule);
}
#endif