File: mpi_Op.c

package info (click to toggle)
openmpi 5.0.7-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 202,312 kB
  • sloc: ansic: 612,441; makefile: 42,495; sh: 11,230; javascript: 9,244; f90: 7,052; java: 6,404; perl: 5,154; python: 1,856; lex: 740; fortran: 61; cpp: 20; tcl: 12
file content (173 lines) | stat: -rw-r--r-- 5,531 bytes parent folder | download | duplicates (4)
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
/*
 * Copyright (c) 2004-2007 The Trustees of Indiana University and Indiana
 *                         University Research and Technology
 *                         Corporation.  All rights reserved.
 * Copyright (c) 2004-2005 The University of Tennessee and The University
 *                         of Tennessee Research Foundation.  All rights
 *                         reserved.
 * Copyright (c) 2004-2005 High Performance Computing Center Stuttgart,
 *                         University of Stuttgart.  All rights reserved.
 * Copyright (c) 2004-2005 The Regents of the University of California.
 *                         All rights reserved.
 * Copyright (c) 2015      Los Alamos National Security, LLC. All rights
 *                         reserved.
 * Copyright (c) 2018      Research Organization for Information Science
 *                         and Technology (RIST).  All rights reserved.
 * $COPYRIGHT$
 *
 * Additional copyrights may follow
 *
 * $HEADER$
 */
/*
 * This file is almost a complete re-write for Open MPI compared to the
 * original mpiJava package. Its license and copyright are listed below.
 * See <path to ompi/mpi/java/README> for more information.
 */
/*
    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.
*/

/*
 * File         : mpi_Op.c
 * Headerfile   : mpi_Op.h
 * Author       : Xinying Li, Bryan Carpenter
 * Created      : Thu Apr  9 12:22:15 1998
 * Revision     : $Revision: 1.7 $
 * Updated      : $Date: 2003/01/16 16:39:34 $
 * Copyright: Northeast Parallel Architectures Center
 *            at Syracuse University 1998
 */
#include "ompi_config.h"

#ifdef HAVE_TARGETCONDITIONALS_H
#include <TargetConditionals.h>
#endif

#include "mpi.h"
#include "mpi_Op.h"
#include "mpiJava.h"
#include "ompi/op/op.h"

JNIEXPORT void JNICALL Java_mpi_Op_init(JNIEnv *env, jclass clazz)
{
    ompi_java.OpHandle  = (*env)->GetFieldID(env, clazz, "handle", "J");
    ompi_java.OpCommute = (*env)->GetFieldID(env, clazz, "commute", "Z");

    ompi_java.OpCall = (*env)->GetMethodID(env, clazz, "call",
                       "(Ljava/lang/Object;Ljava/lang/Object;I)V");
}

JNIEXPORT void JNICALL Java_mpi_Op_getOp(JNIEnv *env, jobject jthis, jint type)
{
    static MPI_Op Ops[] = {
        MPI_OP_NULL, MPI_MAX, MPI_MIN, MPI_SUM,
        MPI_PROD, MPI_LAND, MPI_BAND, MPI_LOR, MPI_BOR, MPI_LXOR,
        MPI_BXOR, MPI_MINLOC, MPI_MAXLOC, MPI_REPLACE, MPI_NO_OP
    };
    (*env)->SetLongField(env,jthis, ompi_java.OpHandle, (jlong)Ops[type]);
}

static jobject setBooleanArray(JNIEnv *env, void *vec, int len)
{
    jobject obj = (*env)->NewBooleanArray(env, len);

    if(obj != NULL)
        (*env)->SetBooleanArrayRegion(env, obj, 0, len, vec);

    return obj;
}

static void getBooleanArray(JNIEnv *env, jobject obj, void *vec, int len)
{
    (*env)->GetBooleanArrayRegion(env, obj, 0, len, vec);
}

static void opIntercept(void *invec, void *inoutvec, int *count,
                        MPI_Datatype *datatype, int baseType,
                        void *jnienv, void *object)
{
    JNIEnv  *env  = jnienv;
    jobject jthis = object;
    jobject jin, jio;

    MPI_Aint lb, extent;
    int rc = MPI_Type_get_extent(*datatype, &lb, &extent);

    if(ompi_java_exceptionCheck(env, rc))
        return;

    int len = (*count) * extent;

    if(baseType == 4)
    {
        jin = setBooleanArray(env, invec, len);
        jio = setBooleanArray(env, inoutvec, len);
    }
    else
    {
        jin = (*env)->NewDirectByteBuffer(env, invec, len);
        jio = (*env)->NewDirectByteBuffer(env, inoutvec, len);
    }

    if((*env)->ExceptionCheck(env))
        return;

    (*env)->CallVoidMethod(env, jthis, ompi_java.OpCall, jin, jio, *count);

    if(baseType == 4)
        getBooleanArray(env, jio, inoutvec, len);

    (*env)->DeleteLocalRef(env, jin);
    (*env)->DeleteLocalRef(env, jio);
}

MPI_Op ompi_java_op_getHandle(JNIEnv *env, jobject jOp, jlong hOp, int baseType)
{
    MPI_Op op = (MPI_Op)hOp;

    if(op == NULL)
    {
        /* It is an uninitialized user Op. */
        int commute = (*env)->GetBooleanField(
                      env, jOp, ompi_java.OpCommute);

        int rc = MPI_Op_create((MPI_User_function*)opIntercept, commute, &op);

        if(ompi_java_exceptionCheck(env, rc))
            return NULL;

        (*env)->SetLongField(env, jOp, ompi_java.OpHandle, (jlong)op);
        ompi_op_set_java_callback(op, env, jOp, baseType);
    }

    return op;
}

JNIEXPORT void JNICALL Java_mpi_Op_free(JNIEnv *env, jobject jthis)
{
    MPI_Op op = (MPI_Op)((*env)->GetLongField(env, jthis, ompi_java.OpHandle));

    if(op != NULL && op != MPI_OP_NULL)
    {
        int rc = MPI_Op_free(&op);
        ompi_java_exceptionCheck(env, rc);
        ((*env)->SetLongField(env,jthis,ompi_java.OpHandle,(long)MPI_OP_NULL));
    }
}

JNIEXPORT jboolean JNICALL Java_mpi_Op_isNull(JNIEnv *env, jobject jthis)
{
    MPI_Op op = (MPI_Op)((*env)->GetLongField(env, jthis, ompi_java.OpHandle));
    return op == NULL || op == MPI_OP_NULL ? JNI_TRUE : JNI_FALSE;
}