From 1f8f0a33a908893a1eb63a788ec1e40e4d858d48 Mon Sep 17 00:00:00 2001
From: Chris Maffeo <cmaffeo2@illinois.edu>
Date: Sun, 11 Aug 2024 15:48:24 -0500
Subject: [PATCH] Add Resource class

---
 README.md          |  5 +++-
 src/CMakeLists.txt |  1 +
 src/Patch.h        |  1 +
 src/Proxy.h        | 39 +-------------------------
 src/Resource.cu    | 15 ++++++++++
 src/Resource.h     | 68 ++++++++++++++++++++++++++++++++++++++++++++++
 src/Types.h        |  3 ++
 7 files changed, 93 insertions(+), 39 deletions(-)
 create mode 100644 src/Resource.cu
 create mode 100644 src/Resource.h

diff --git a/README.md b/README.md
index 6863d76..1f6f312 100644
--- a/README.md
+++ b/README.md
@@ -19,9 +19,12 @@ Linux workstation with CUDA-compatible GPU (minimum 3.5 compute capability)
   - CMake >= 3.9
   - gcc >= 4.9
   - cuda >= 9.0  (> 11.5 recommended)
+  - spdlog >= 1.10.0 (note: this is normally installed to extern/spdlog by running `git submodule update --init` from this directory)
 
 ### Build process
 
+From the root arbd directory (where this README is found), ensure you have spdlog installed to the extern directory, usually by running `git submodule update --init`.
+
 From the root arbd directory (where this README is found), run:
 ```
 ## Determine the compute capability of your CUDA-enabled graphics card
@@ -52,4 +55,4 @@ ARBD2 is being developed by the Aksimentiev group
   - Christopher Maffeo <mailto:cmaffeo2@illinois.edu>
   - Han-yi Chao
 
-Please direct questions or problems to Chris.
\ No newline at end of file
+Please direct questions, problems or suggestions to Chris.
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt
index 5810549..342243c 100644
--- a/src/CMakeLists.txt
+++ b/src/CMakeLists.txt
@@ -1,6 +1,7 @@
 
 add_library("lib${PROJECT_NAME}"
   ARBDException.cpp
+  Resource.cu
   GPUManager.cpp
   ParticlePatch.cpp
   SimSystem.cpp
diff --git a/src/Patch.h b/src/Patch.h
index f7559f5..cd211f9 100644
--- a/src/Patch.h
+++ b/src/Patch.h
@@ -39,6 +39,7 @@ private:
     // Particle data
     size_t num_particles;
 
+    idx_t* global_idx;		// global index of particle
     size_t* type_ids;
     Pos* pos;
     Force* force;
diff --git a/src/Proxy.h b/src/Proxy.h
index 3f3dd04..e1c49e5 100644
--- a/src/Proxy.h
+++ b/src/Proxy.h
@@ -2,44 +2,7 @@
 
 #include <future>
 #include <iostream>
-#include "ARBDException.h"
-
-/**
- * @brief Represents a resource that can store data and perform computations.
- */
-struct Resource {
-    /**
-     * @brief Enum to specify the type of the resource (e.g., CPU or GPU).
-     */
-    enum ResourceType {CPU, MPI, GPU};
-    ResourceType type; ///< Type of the resource.
-    size_t id; ///< ID or any other identifier associated with the resource.
-
-    // Q: should this return True for GPU that is attached/assigned to current thread? Currently assuming yes.
-    HOST DEVICE bool is_local() const {
-	bool ret = true;
-// #ifdef __CUDA_ACC__
-// 	ret = (type == GPU);
-// #else
-// 	ret = (type == CPU);
-// #endif
-	LOGWARN("Resource::is_local() not fully implemented; returning {}",ret);
-	return ret;
-    };
-    // HOST DEVICE static bool is_local() { // check if thread/gpu idx matches some global idx };
-
-    static Resource Local() {
-	LOGWARN("Resource::Local() not properly implemented");
-#ifdef __CUDA_ACC__
-	return Resource{ GPU, 0 };
-#else
-	return Resource{ CPU, 0 };
-#endif
-    };
-    bool operator==(const Resource& other) const { return type == other.type && id == other.id; };
-	
-};
-
+#include "Resource.h"
 
 // START traits
 // These ugly bits of code help implement SFINAE in C++14 and should likely be removed if a newer standard is adopted 
diff --git a/src/Resource.cu b/src/Resource.cu
new file mode 100644
index 0000000..bff4d60
--- /dev/null
+++ b/src/Resource.cu
@@ -0,0 +1,15 @@
+#include "Resource.h"
+
+HOST DEVICE size_t current_thread_idx() {
+#ifdef USE_MPI
+    Exception( NotImplementedError, "current_thread_idx() not implemented on GPU" );
+    int world_rank;
+    return MPI_Comm_rank(MPI_COMM_WORLD, &world_rank);
+#else
+#ifdef __CUDA_ACC__
+    Exception( NotImplementedError, "current_thread_idx() not implemented on GPU" );
+#else
+    return 0;
+#endif
+#endif
+}
diff --git a/src/Resource.h b/src/Resource.h
new file mode 100644
index 0000000..8a6b054
--- /dev/null
+++ b/src/Resource.h
@@ -0,0 +1,68 @@
+#pragma once
+
+#ifdef USE_MPI
+#include <mpi.h>
+#endif
+
+#include "ARBDException.h"
+#include "GPUManager.h"
+
+
+HOST DEVICE size_t current_thread_idx();
+    
+
+
+/**
+ * @brief Represents a resource that can store data and perform computations.
+ */
+struct Resource {
+    /**
+     * @brief Enum to specify the type of the resource (e.g., CPU or GPU).
+     */
+    enum ResourceType {CPU, MPI, GPU};
+    ResourceType type; ///< Type of the resource.
+    size_t id; ///< Unique identifier associated with the resource.
+    Resource* parent; ///< Parent resource; nullptr unless type is GPU
+        
+    /**
+     * @brief Checks if the resource is running on the calling thread. 
+     */
+    HOST DEVICE bool is_running() const {
+	bool ret = true;
+// #ifdef __CUDA_ACC__
+// 	ret = (type == GPU);
+// #else
+// 	ret = (type == CPU);
+// #endif
+	return ret;
+    }
+
+    // Q: should this return True for GPU that is attached/assigned to current thread? Currently assuming yes.
+    HOST DEVICE bool is_local() const {
+	bool ret = true;
+#ifdef __CUDA_ACC__
+	ret = (type == GPU);
+	LOGWARN("Resource::is_local() not fully implemented on GPU devices; returning {}",ret);
+#else
+	if (type == GPU && parent != nullptr) {
+	    ret = parent->is_local();
+	} else {
+	    ret = (current_thread_idx() == id);
+	}
+#endif
+	return ret;
+    };
+    // HOST DEVICE static bool is_local() { // check if thread/gpu idx matches some global idx };
+
+    static Resource Local() {
+	LOGWARN("Resource::Local() not properly implemented");
+#ifdef __CUDA_ACC__
+	return Resource{ GPU, 0 };
+#else
+	return Resource{ CPU, 0 };
+#endif
+    };
+    bool operator==(const Resource& other) const { return type == other.type && id == other.id; };
+	
+};
+
diff --git a/src/Types.h b/src/Types.h
index 9213ebb..e42a83c 100644
--- a/src/Types.h
+++ b/src/Types.h
@@ -55,3 +55,6 @@ HOST DEVICE inline Vector3_t<size_t> index_to_ijk(size_t idx, const Vector3_t<si
     return index_to_ijk(idx, n.x, n.y, n.z);
 }
 
+using idx_t = size_t;		/* We will sometimes refer to global
+				 * particle index, which may be too
+				 * large to represent via size_t */
-- 
GitLab