-
Notifications
You must be signed in to change notification settings - Fork 131
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Backport 2.x] Introduce a loading layer in NMSLIB. (#2185) #2220
Merged
shatejas
merged 3 commits into
opensearch-project:2.x
from
0ctopus13prime:backport-2185-to-2.x
Oct 21, 2024
+1,108
−564
Merged
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
/* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* The OpenSearch Contributors require contributions made to | ||
* this file be licensed under the Apache-2.0 license or a | ||
* compatible open source license. | ||
* | ||
* Modifications Copyright OpenSearch Contributors. See | ||
* GitHub history for details. | ||
*/ | ||
|
||
#ifndef OPENSEARCH_KNN_JNI_STREAM_SUPPORT_H | ||
#define OPENSEARCH_KNN_JNI_STREAM_SUPPORT_H | ||
|
||
#include "jni_util.h" | ||
|
||
#include <jni.h> | ||
#include <stdexcept> | ||
#include <iostream> | ||
#include <cstring> | ||
|
||
namespace knn_jni { | ||
namespace stream { | ||
|
||
|
||
|
||
/** | ||
* This class contains Java IndexInputWithBuffer reference and calls its API to copy required bytes into a read buffer. | ||
*/ | ||
class NativeEngineIndexInputMediator { | ||
public: | ||
// Expect IndexInputWithBuffer is given as `_indexInput`. | ||
NativeEngineIndexInputMediator(JNIUtilInterface *_jni_interface, | ||
JNIEnv *_env, | ||
jobject _indexInput) | ||
: jni_interface(_jni_interface), | ||
env(_env), | ||
indexInput(_indexInput), | ||
bufferArray((jbyteArray) (_jni_interface->GetObjectField(_env, | ||
_indexInput, | ||
getBufferFieldId(_jni_interface, _env)))), | ||
copyBytesMethod(getCopyBytesMethod(_jni_interface, _env)), | ||
remainingBytesMethod(getRemainingBytesMethod(_jni_interface, _env)) { | ||
} | ||
|
||
void copyBytes(int64_t nbytes, uint8_t *destination) { | ||
auto jclazz = getIndexInputWithBufferClass(jni_interface, env); | ||
|
||
while (nbytes > 0) { | ||
// Call `copyBytes` to read bytes as many as possible. | ||
jvalue args; | ||
args.j = nbytes; | ||
const auto readBytes = | ||
jni_interface->CallNonvirtualIntMethodA(env, indexInput, jclazz, copyBytesMethod, &args); | ||
|
||
// === Critical Section Start === | ||
|
||
// Get primitive array pointer, no copy is happening in OpenJDK. | ||
auto primitiveArray = | ||
(jbyte *) jni_interface->GetPrimitiveArrayCritical(env, bufferArray, nullptr); | ||
|
||
// Copy Java bytes to C++ destination address. | ||
std::memcpy(destination, primitiveArray, readBytes); | ||
|
||
// Release the acquired primitive array pointer. | ||
// JNI_ABORT tells JVM to directly free memory without copying back to Java byte[]. | ||
// Since we're merely copying data, we don't need to copying back. | ||
jni_interface->ReleasePrimitiveArrayCritical(env, bufferArray, primitiveArray, JNI_ABORT); | ||
|
||
// === Critical Section End === | ||
|
||
destination += readBytes; | ||
nbytes -= readBytes; | ||
} // End while | ||
} | ||
|
||
int64_t remainingBytes() { | ||
return jni_interface->CallNonvirtualLongMethodA(env, | ||
indexInput, | ||
getIndexInputWithBufferClass(jni_interface, env), | ||
remainingBytesMethod, | ||
nullptr); | ||
} | ||
|
||
private: | ||
static jclass getIndexInputWithBufferClass(JNIUtilInterface *jni_interface, JNIEnv *env) { | ||
static jclass INDEX_INPUT_WITH_BUFFER_CLASS = | ||
jni_interface->FindClassFromJNIEnv(env, "org/opensearch/knn/index/store/IndexInputWithBuffer"); | ||
return INDEX_INPUT_WITH_BUFFER_CLASS; | ||
} | ||
|
||
static jmethodID getCopyBytesMethod(JNIUtilInterface *jni_interface, JNIEnv *env) { | ||
static jmethodID COPY_METHOD_ID = | ||
jni_interface->GetMethodID(env, getIndexInputWithBufferClass(jni_interface, env), "copyBytes", "(J)I"); | ||
return COPY_METHOD_ID; | ||
} | ||
|
||
static jmethodID getRemainingBytesMethod(JNIUtilInterface *jni_interface, JNIEnv *env) { | ||
static jmethodID COPY_METHOD_ID = | ||
jni_interface->GetMethodID(env, getIndexInputWithBufferClass(jni_interface, env), "remainingBytes", "()J"); | ||
return COPY_METHOD_ID; | ||
} | ||
|
||
static jfieldID getBufferFieldId(JNIUtilInterface *jni_interface, JNIEnv *env) { | ||
static jfieldID BUFFER_FIELD_ID = | ||
jni_interface->GetFieldID(env, getIndexInputWithBufferClass(jni_interface, env), "buffer", "[B"); | ||
return BUFFER_FIELD_ID; | ||
} | ||
|
||
JNIUtilInterface *jni_interface; | ||
JNIEnv *env; | ||
|
||
// `IndexInputWithBuffer` instance having `IndexInput` instance obtained from `Directory` for reading. | ||
jobject indexInput; | ||
jbyteArray bufferArray; | ||
jmethodID copyBytesMethod; | ||
jmethodID remainingBytesMethod; | ||
}; // class NativeEngineIndexInputMediator | ||
|
||
|
||
|
||
} | ||
} | ||
|
||
#endif //OPENSEARCH_KNN_JNI_STREAM_SUPPORT_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
/* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* The OpenSearch Contributors require contributions made to | ||
* this file be licensed under the Apache-2.0 license or a | ||
* compatible open source license. | ||
* | ||
* Modifications Copyright OpenSearch Contributors. See | ||
* GitHub history for details. | ||
*/ | ||
|
||
#ifndef OPENSEARCH_KNN_JNI_NMSLIB_STREAM_SUPPORT_H | ||
#define OPENSEARCH_KNN_JNI_NMSLIB_STREAM_SUPPORT_H | ||
|
||
#include "native_engines_stream_support.h" | ||
|
||
namespace knn_jni { | ||
namespace stream { | ||
|
||
|
||
|
||
/** | ||
* NmslibIOReader implementation delegating NativeEngineIndexInputMediator to read bytes. | ||
*/ | ||
class NmslibOpenSearchIOReader final : public similarity::NmslibIOReader { | ||
public: | ||
explicit NmslibOpenSearchIOReader(NativeEngineIndexInputMediator *_mediator) | ||
: mediator(_mediator) { | ||
} | ||
|
||
void read(char *bytes, size_t len) final { | ||
if (len > 0) { | ||
// Mediator calls IndexInput, then copy read bytes to `ptr`. | ||
mediator->copyBytes(len, (uint8_t *) bytes); | ||
} | ||
} | ||
|
||
size_t remainingBytes() final { | ||
return mediator->remainingBytes(); | ||
} | ||
|
||
private: | ||
NativeEngineIndexInputMediator *mediator; | ||
}; // class NmslibOpenSearchIOReader | ||
|
||
|
||
|
||
} | ||
} | ||
|
||
#endif //OPENSEARCH_KNN_JNI_NMSLIB_STREAM_SUPPORT_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should be in 2.x