Skip to content

Instantly share code, notes, and snippets.

psyq321's "conversations" with GPT-4

In this series, I am casually asking GPT-4 to solve some old problems, mainly focusing on software optimization and algorithm development. The goal is to show the capability of the model to understand natural language and solve easy to medium problems one would typically find in day-to-day work in software engineering.


QUESTION:

Using C++ 20 and AVX (AVX, AVX2 and AVX-512) compiler intrinsics implement fast calculation of distance between a 3D point and a line segment, using 32-bit float precision

psyq321's "conversations" with GPT-4

In this series, I am casually asking GPT-4 to solve some old problems, mainly focusing on software optimization and algorithm development. The goal is to show the capability of the model to understand natural language and solve easy to medium problems one would typically find in day-to-day work in software engineering.


QUESTION:

Performance-critical part of the simulation needs to perform collision checks on the 3D volume occupied by simulated cells. The performance-critical part of the simulation models the cell's axon and dendrites as a set of free-oriented cylinders in 3D space (each cylinder is a “compartment”).

QUESTION: User is simulating biological tissue, described as 3D surface mesh, where each mesh triangle has an assigned set of "depth" values each representing the thickness of subsurface layer forming the volume using sections of inverse surface normal vector (pointing "inwards" into the mesh) - for example, neocortex has 6 layers, so each cortical mesh triangle will have 6 floating point values associated in addition to the surface normals. A cell is a 3D point inside the volume described by the triangle and associated layer depth(s).

In C++ 20, and using GLM math library, implement routine that created X (number provided by user) cells inside the volume described by the surface mesh and corresponding layer information (number of layers, thickness of each layer and surface normal(s)) ensuring:

a) Fair coverage of triangles b) Close to homogeneous volume between layers and triangles (cell concentration)

--

GPT-4 ANSWER:

QUESTION: Using C++ 20 and glm math library only, implement sorting of a set of 3D points using the complete Hilbert space filling curve implementation, including 3D rotation and reflection. For simplicity sake, all coordinates of the 3D points are positive, but each point can have arbitrary floating point value (not just integers), but the bounding box containing all points is known beforehand.

GPT-4 ANSWER:

Here is an implementation that uses a classic 3D Hilbert curve mapping to sort points. Note that this does handle floating point coordinates but the entire space is discretized to the closest integer to compute the Hilbert value.

This example uses the boost library (Precompiled library available for Visual Studio in vcpkg).

#include <iostream>

QUESTION: Neural network simulator operates on a 1D array of neurons, each defined as a structure containing a 3D point, parent cortical area and associated synaptic weights. Before simulation starts, implement sorting algorithm to maximize cache locality in processing using Hilbert or Morton space-filling curves. Please use plain C++ 20 and glm library.

GPT-4 ANSWER:

Sure, first you should install and include the following libraries in your C++ program:

#include <glm/glm.hpp>                         // Include glm library
#include <vector>
#include <array>

This problem can be solved by using a greedy selection algorithm which combines information from both criteria mentioned above to select tracts.

Two class definitions are used to hold information associated with tracts and triangles:

struct Tract {
    glm::vec3 start;
    glm::vec3 end;
    uint32_t startTriangleInd;
    uint32_t endTriangleInd;

This problem could be modelled as a variant of the Maximal Coverage (MC) model with a distance metric added into it. However, practical solution for the problem would require sophisticated approaches, such as spatial analytics, clustering, and graph theory algorithms. Here I will give a high-level idea how this can be implemented:

We may start with a brute force approach, calculate triangle coverage and dispersion for each possible subset of tracts and finally select the one maximizing coverage and dispersion. However, this approach is computationally expensive.

Instead, I suggest the following approach that uses k-means clustering algorithm for computation efficiency:

  1. Compute the centroid for each tract by averaging its start and end points.
  2. Use k-means clustering algorithm over the tract centroids where k is the number of requested tracts. This will give us k clusters of tracts.
  3. For each cluster, select the tract that is closest to the cluster centroid, this tract will maximize the dispersion.

To implement the described algorithm, we'll use an iterative approach where we first calculate the closest triangles for both the beginning and ending points of each tract and then choose the subset of tracts that maximize the mesh coverage and area dispersion. For this, we will create helper functions to find the closest triangle, calculate dispersion, and select the subset of tracts.

Include the necessary headers:

#include <iostream>
#include <vector>
#include <algorithm>
#include <limits>
#include <tuple>