~ubuntu-branches/ubuntu/utopic/gridengine/utopic

« back to all changes in this revision

Viewing changes to source/libs/japi/test_drmaa_mcpu.c

  • Committer: Bazaar Package Importer
  • Author(s): Mark Hymers
  • Date: 2008-06-25 22:36:13 UTC
  • Revision ID: james.westby@ubuntu.com-20080625223613-tvd9xlhuoct9kyhm
Tags: upstream-6.2~beta2
ImportĀ upstreamĀ versionĀ 6.2~beta2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#include <time.h>
 
2
#include <sys/types.h>
 
3
#include <pthread.h>
 
4
#include <stdio.h>
 
5
#include <stdlib.h>
 
6
#include "drmaa.h"
 
7
 
 
8
#define CELL "default"
 
9
#define WD "/tmp"
 
10
#define CMD "/tmp/sleeper.sh"
 
11
#define CATEGORY "test"
 
12
 
 
13
int handle_code(int code, char *msg, int r, int t);
 
14
void *run(void *arg);
 
15
 
 
16
int main(int argc, char **argv) {
 
17
    int ret = DRMAA_ERRNO_SUCCESS;
 
18
    char error[DRMAA_ERROR_STRING_BUFFER + 1];
 
19
    
 
20
    int runs = 20;
 
21
    int threads = 50;
 
22
    int run_count = 0;
 
23
    int thread_count = 0;
 
24
    pthread_t *ids = NULL;
 
25
    
 
26
    ids = (pthread_t *)malloc(sizeof (pthread_t) * threads);
 
27
    
 
28
    ret = drmaa_init(CELL, error, DRMAA_ERROR_STRING_BUFFER);
 
29
    if (handle_code(ret, error, -1, 0) == 1) {
 
30
        exit(1);
 
31
    }
 
32
    
 
33
    for (run_count = 0; run_count < runs; run_count++) {
 
34
        printf("-1 0 STARTING RUN %d %ld\n", run_count, time(NULL));
 
35
        
 
36
        for (thread_count = 0; thread_count < threads; thread_count++) {
 
37
            int *arg = (int *)malloc(sizeof (int) * 2);
 
38
            
 
39
            arg[0] = run_count;
 
40
            arg[1] = thread_count;
 
41
            
 
42
            if (pthread_create(&ids[thread_count], NULL, run, arg) != 0) {
 
43
                printf("%d %d EXCEPTION: Couldn't create thread %ld\n", run_count,
 
44
                thread_count, time(NULL));
 
45
            }
 
46
        }
 
47
        
 
48
        for (thread_count = 0; thread_count < threads; thread_count++) {
 
49
            pthread_join(ids[thread_count], NULL);
 
50
        }
 
51
        
 
52
        printf("-1 0 ENDING RUN %d %ld\n", run_count, time(NULL));
 
53
    }
 
54
    
 
55
    ret = drmaa_exit(error, DRMAA_ERROR_STRING_BUFFER);
 
56
    handle_code(ret, error, -1, 0);
 
57
    
 
58
    return 0;
 
59
}
 
60
 
 
61
int handle_code(int code, char *msg, int r, int t) {
 
62
    if (code != DRMAA_ERRNO_SUCCESS) {
 
63
        printf("%d %d EXCEPTION: %s %ld\n", r, t, msg, time(NULL));
 
64
        return 1;
 
65
    }
 
66
    else {
 
67
        return 0;
 
68
    }
 
69
}
 
70
 
 
71
void *run(void *arg) {
 
72
    int ret = DRMAA_ERRNO_SUCCESS;
 
73
    char error[DRMAA_ERROR_STRING_BUFFER + 1];
 
74
    
 
75
    drmaa_job_template_t *jt = NULL;
 
76
    int run = ((int *)arg)[0];
 
77
    int thread = ((int *)arg)[1];
 
78
    char jobid[DRMAA_JOBNAME_BUFFER + 1];
 
79
    int queued = 1;
 
80
    int running = 0;
 
81
    int status = -1;
 
82
    
 
83
    free(arg);
 
84
    
 
85
    ret = drmaa_allocate_job_template(&jt, error, DRMAA_ERROR_STRING_BUFFER);
 
86
    if (handle_code(ret, error, run, thread) == 1) {
 
87
        return NULL;
 
88
    }
 
89
    
 
90
    ret = drmaa_set_attribute(jt, DRMAA_REMOTE_COMMAND, CMD, error,
 
91
    DRMAA_ERROR_STRING_BUFFER);
 
92
    if (handle_code(ret, error, run, thread) == 1) {
 
93
        return NULL;
 
94
    }
 
95
    
 
96
    ret = drmaa_set_attribute(jt, DRMAA_WD, WD, error,
 
97
    DRMAA_ERROR_STRING_BUFFER);
 
98
    if (handle_code(ret, error, run, thread) == 1) {
 
99
        return NULL;
 
100
    }
 
101
    
 
102
    ret = drmaa_set_attribute(jt, DRMAA_JOB_CATEGORY, CATEGORY, error,
 
103
    DRMAA_ERROR_STRING_BUFFER);
 
104
    if (handle_code(ret, error, run, thread) == 1) {
 
105
        return NULL;
 
106
    }
 
107
    
 
108
    printf("%d %d SETUP complete %ld\n", run, thread, time(NULL));
 
109
    
 
110
    ret = drmaa_run_job(jobid, DRMAA_JOBNAME_BUFFER, jt, error,
 
111
    DRMAA_ERROR_STRING_BUFFER);
 
112
    if (handle_code(ret, error, run, thread) == 1) {
 
113
        return NULL;
 
114
    }
 
115
    
 
116
    printf("%d %d SUBMITTED jobid: %s %ld\n", run, thread, jobid, time(NULL));
 
117
    
 
118
    ret = drmaa_delete_job_template(jt, error, DRMAA_ERROR_STRING_BUFFER);
 
119
    handle_code(ret, error, run, thread);
 
120
    
 
121
    while (queued) {
 
122
        ret = drmaa_wait(jobid, NULL, 0, NULL, 2, NULL, error,
 
123
        DRMAA_ERROR_STRING_BUFFER);
 
124
        
 
125
        if (ret != DRMAA_ERRNO_EXIT_TIMEOUT) {
 
126
            if (handle_code(ret, error, run, thread) == 1) {
 
127
                return NULL;
 
128
            }
 
129
        }
 
130
        else {
 
131
            printf ("%d %d TIMEOUT jobid: %s %ld\n", run, thread, jobid, time (NULL));
 
132
        }
 
133
        
 
134
        ret = drmaa_job_ps(jobid, &status, error, DRMAA_ERROR_STRING_BUFFER);
 
135
        if (handle_code(ret, error, run, thread) == 1) {
 
136
            return NULL;
 
137
        }
 
138
        
 
139
        queued = (status == DRMAA_PS_QUEUED_ACTIVE) ||
 
140
        (status == DRMAA_PS_SYSTEM_ON_HOLD) ||
 
141
        (status == DRMAA_PS_SYSTEM_ON_HOLD) ||
 
142
        (status == DRMAA_PS_USER_ON_HOLD) ||
 
143
        (status == DRMAA_PS_USER_SYSTEM_ON_HOLD);
 
144
    }
 
145
    
 
146
    printf("%d %d RUNNING jobid: %s %ld\n", run, thread, jobid, time(NULL));
 
147
    
 
148
    running = 1;
 
149
    
 
150
    while (running == 1) {
 
151
        ret = drmaa_wait(jobid, NULL, 0, NULL, 60, NULL, error,
 
152
                         DRMAA_ERROR_STRING_BUFFER);
 
153
 
 
154
        if (ret != DRMAA_ERRNO_EXIT_TIMEOUT) {
 
155
            if (handle_code(ret, error, run, thread) == 1) {
 
156
                return NULL;
 
157
            }
 
158
            
 
159
            running = 0;
 
160
            
 
161
            printf("%d %d FINISHED jobid: %s %ld\n", run, thread, jobid,
 
162
            time(NULL));
 
163
        }
 
164
        else {
 
165
            printf ("%d %d TIMEOUT jobid: %s %ld\n", run, thread, jobid, time (NULL));
 
166
 
 
167
            ret = drmaa_job_ps(jobid, &status, error, DRMAA_ERROR_STRING_BUFFER);
 
168
            
 
169
            if (handle_code(ret, error, run, thread) == 1) {
 
170
                return NULL;
 
171
            }
 
172
            
 
173
            if (status != DRMAA_PS_RUNNING) {
 
174
                running = 0;
 
175
                
 
176
                printf("%d %d HUNG jobid: %s %ld\n", run, thread, jobid, time(NULL));
 
177
            }
 
178
        }
 
179
    }
 
180
    
 
181
    return NULL;
 
182
}