~ubuntu-branches/ubuntu/utopic/nwchem/utopic

« back to all changes in this revision

Viewing changes to src/tools/ga-5-2/pario/dra/buffers.c

  • Committer: Package Import Robot
  • Author(s): Michael Banck, Daniel Leidert, Andreas Tille, Michael Banck
  • Date: 2013-07-04 12:14:55 UTC
  • mfrom: (1.1.2)
  • Revision ID: package-import@ubuntu.com-20130704121455-5tvsx2qabor3nrui
Tags: 6.3-1
* New upstream release.
* Fixes anisotropic properties (Closes: #696361).
* New features include:
  + Multi-reference coupled cluster (MRCC) approaches
  + Hybrid DFT calculations with short-range HF 
  + New density-functionals including Minnesota (M08, M11) and HSE hybrid
    functionals
  + X-ray absorption spectroscopy (XAS) with TDDFT
  + Analytical gradients for the COSMO solvation model
  + Transition densities from TDDFT 
  + DFT+U and Electron-Transfer (ET) methods for plane wave calculations
  + Exploitation of space group symmetry in plane wave geometry optimizations
  + Local density of states (LDOS) collective variable added to Metadynamics
  + Various new XC functionals added for plane wave calculations, including
    hybrid and range-corrected ones
  + Electric field gradients with relativistic corrections 
  + Nudged Elastic Band optimization method
  + Updated basis sets and ECPs 

[ Daniel Leidert ]
* debian/watch: Fixed.

[ Andreas Tille ]
* debian/upstream: References

[ Michael Banck ]
* debian/upstream (Name): New field.
* debian/patches/02_makefile_flags.patch: Refreshed.
* debian/patches/06_statfs_kfreebsd.patch: Likewise.
* debian/patches/07_ga_target_force_linux.patch: Likewise.
* debian/patches/05_avoid_inline_assembler.patch: Removed, no longer needed.
* debian/patches/09_backported_6.1.1_fixes.patch: Likewise.
* debian/control (Build-Depends): Added gfortran-4.7 and gcc-4.7.
* debian/patches/10_force_gcc-4.7.patch: New patch, explicitly sets
  gfortran-4.7 and gcc-4.7, fixes test suite hang with gcc-4.8 (Closes:
  #701328, #713262).
* debian/testsuite: Added tests for COSMO analytical gradients and MRCC.
* debian/rules (MRCC_METHODS): New variable, required to enable MRCC methods.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#if HAVE_CONFIG_H
 
2
#   include "config.h"
 
3
#endif
 
4
 
 
5
/** @file
 
6
 * Buffer Manager for managing buffers in any application
 
7
 */
 
8
 
 
9
#if HAVE_STDIO_H
 
10
#   include <stdio.h>
 
11
#endif
 
12
#if HAVE_STDLIB_H
 
13
#   include <stdlib.h>
 
14
#endif
 
15
#if HAVE_STRINGS_H
 
16
#   include <strings.h>
 
17
#endif
 
18
 
 
19
#include "buffers.h"
 
20
 
 
21
/*#define STATBUF 1 */ /* set if static buffers will be used */
 
22
/*#define DEBUG 1 */
 
23
 
 
24
#define MAX_CTXT 5
 
25
 
 
26
int ctxt_count = 0;
 
27
 
 
28
 
 
29
/** buffer management initialization routine */
 
30
void buffer_init(
 
31
        buf_context_t *ctxt, int nbuf, int buf_size, void (*fptr)(char*))
 
32
{
 
33
    int i;
 
34
    long diff;
 
35
    if (nbuf < 1 || nbuf > MAXBUF) {
 
36
        printf("Too many (or too few) buffers requested, using default number (%d) of buffers", DEFBUF);
 
37
        nbuf = DEFBUF;
 
38
    }
 
39
 
 
40
    /* create a context */
 
41
    ctxt->ctxt_id = ctxt_count++;
 
42
    if (ctxt->ctxt_id > MAX_CTXT) {
 
43
        printf("Max number of contexts reached!\n");
 
44
        return;
 
45
    }
 
46
 
 
47
    ctxt->nbuf = nbuf;
 
48
    ctxt->size = buf_size;
 
49
    ctxt->buf = (_buffer_t *)malloc(sizeof(_buffer_t) * nbuf);
 
50
    ctxt->fptr = fptr;
 
51
 
 
52
#ifdef STATBUF
 
53
    double buffers[MAXBUF][DBL_BUF_SIZE];
 
54
    for (i = 0; i < nbuf; i++) { 
 
55
        ctxt->buf[i].buffer = (char *) buffers[i];
 
56
        bzero(buffers[i], sizeof(buffers[i]));
 
57
    }
 
58
#else /* STATBUF */
 
59
 
 
60
    /* get buffer memory */
 
61
    for (i = 0; i < nbuf; i++) {
 
62
        ctxt->buf[i].buffer = (char*) malloc((buf_size + ALIGN-1) *sizeof(double));
 
63
 
 
64
        if (ctxt->buf[i].buffer == NULL) {
 
65
            printf("Could not allocate memory for buffers!\n");
 
66
            return;
 
67
        }
 
68
        bzero(ctxt->buf[i].buffer, sizeof(ctxt->buf[i].buffer));
 
69
 
 
70
        /* align buffer address */
 
71
        diff = ((long)(ctxt->buf[i].buffer)) % (sizeof(double)*ALIGN);
 
72
        if(diff) 
 
73
        {
 
74
            ctxt->buf[i].align_off = (int) (sizeof(double)*ALIGN - diff);
 
75
        } else 
 
76
        {
 
77
            ctxt->buf[i].align_off = (int) 0;
 
78
        }
 
79
        ctxt->buf[i].buffer += ctxt->buf[i].align_off;
 
80
 
 
81
    }
 
82
#endif /* STATBUF */
 
83
    for (i = 0; i < nbuf; i++) {
 
84
        ctxt->buf[i].active = 0;
 
85
        ctxt->buf[i].group_id = 0;
 
86
    }
 
87
#ifdef DEBUG
 
88
    printf("Created a context\n\n");
 
89
#endif
 
90
}
 
91
 
 
92
 
 
93
/** internal function to return empty buffer handle */
 
94
int get_buf_hdl(buf_context_t *ctxt)
 
95
{
 
96
    int i;
 
97
    for (i = 0; i < ctxt->nbuf; i++) {
 
98
        if (ctxt->buf[i].active == 0) {
 
99
            ctxt->buf[i].active = 1;
 
100
            return i;
 
101
        }
 
102
    }
 
103
    return -1;
 
104
}
 
105
 
 
106
 
 
107
char* get_buf(buf_context_t *ctxt, int call_id)
 
108
{
 
109
    int hdl;
 
110
    char *buf;
 
111
 
 
112
    hdl = get_buf_hdl(ctxt);
 
113
    if (hdl == -1) {
 
114
        int cur_buf;
 
115
        /* no buffer is available, wait for the oldest buffer to become free */
 
116
        cur_buf = (ctxt->last_buf + 1) % ctxt->nbuf;
 
117
 
 
118
        /* free this buffer by calling the callback function provided by the application */
 
119
        ctxt->fptr(ctxt->buf[cur_buf].buffer); 
 
120
        hdl = cur_buf;
 
121
    }
 
122
    buf = ctxt->buf[hdl].buffer;
 
123
    ctxt->buf[hdl].buf_hdl = hdl;
 
124
    ctxt->buf[hdl].call_id = call_id;
 
125
    ctxt->buf[hdl].active = 1;
 
126
    ctxt->last_buf = hdl;
 
127
#ifdef DEBUG
 
128
    printf("Giving a buffer with internal handle: %d\n", hdl);
 
129
#endif
 
130
    return (buf);
 
131
}
 
132
 
 
133
 
 
134
/** function to free a buffer */
 
135
void free_buf(buf_context_t *ctxt, char *buf)
 
136
{
 
137
    int i;
 
138
    for (i = 0; i < ctxt->nbuf; i++) {
 
139
        if (ctxt->buf[i].buffer == buf) {
 
140
            ctxt->buf[i].active = 0;
 
141
            break;
 
142
        }
 
143
    }
 
144
}
 
145
 
 
146
 
 
147
/** function to complete an entire call */
 
148
void buf_complete_call(buf_context_t *ctxt, int call_id)
 
149
{
 
150
    int i;
 
151
#ifdef DEBUG
 
152
    printf("Completing call with call id: %d\n", call_id);
 
153
#endif
 
154
    for (i = 0; i < ctxt->nbuf; i++) {
 
155
        if (ctxt->buf[i].call_id == call_id && ctxt->buf[i].active == 1) {
 
156
            /* force completion by calling user function */
 
157
            ctxt->fptr(ctxt->buf[i].buffer);
 
158
            ctxt->buf[i].active = 0;
 
159
        }
 
160
    }
 
161
}
 
162
 
 
163
 
 
164
/** function to return the call_id associated with a particular buffer */
 
165
int buf_get_call_id(buf_context_t *ctxt, char *buf)
 
166
{
 
167
    int i;
 
168
    for (i = 0; i < ctxt->nbuf; i++)
 
169
        if (ctxt->buf[i].buffer == buf)
 
170
            return(ctxt->buf[i].call_id);
 
171
    printf("Buf_man error: Cannot find call_id for this buffer\n");
 
172
    return -1;
 
173
}
 
174
 
 
175
 
 
176
/**
 
177
 * Function to return an array of buffers associated with a call_id
 
178
 * the last two parameters are output
 
179
 */
 
180
int get_bufs_of_call_id(
 
181
        buf_context_t *ctxt, int call_id, int *n_buf, char *bufs[])
 
182
{
 
183
    int i, count = 0;
 
184
 
 
185
    for (i = 0; i < ctxt->nbuf; i++)
 
186
        if (ctxt->buf[i].call_id == call_id) {
 
187
            bufs[count++] = ctxt->buf[i].buffer;
 
188
        }
 
189
    *n_buf = count;
 
190
    if (*n_buf == 0) {
 
191
#ifdef DEBUG 
 
192
        printf("Buf_man: No active buffer found for call_id %d\n", call_id);
 
193
#endif
 
194
        return -1; /* no buffer found */
 
195
    }
 
196
 
 
197
    return 0; /* success */
 
198
}
 
199
 
 
200
 
 
201
/** terminates an application context */
 
202
void buf_terminate(buf_context_t *ctxt)
 
203
{
 
204
#ifndef STATBUF
 
205
    int i;
 
206
    for (i = 0; i < ctxt->nbuf; i++) {
 
207
        ctxt->buf[i].buffer -= ctxt->buf[i].align_off;
 
208
        free(ctxt->buf[i].buffer);
 
209
    }
 
210
 
 
211
#endif
 
212
 
 
213
    free(ctxt->buf);
 
214
    ctxt_count--; /* this context can be reallocated */
 
215
}