Skip to content

Instantly share code, notes, and snippets.

@altavir
Last active December 29, 2021 07:54
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save altavir/c959ca422288924b493fb7fc4c68220a to your computer and use it in GitHub Desktop.
Save altavir/c959ca422288924b493fb7fc4c68220a to your computer and use it in GitHub Desktop.
A simple matrix mulitplication server inside kotlin notebook
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"datalore": {
"sheet_delimiter": false
}
},
"outputs": [],
"source": [
"@file:DependsOn(\"io.ktor:ktor-server-core:1.6.7\")\n",
"@file:DependsOn(\"io.ktor:ktor-server-cio:1.6.7\")\n",
"@file:DependsOn(\"io.ktor:ktor-serialization:1.6.7\")\n",
"@file:DependsOn(\"io.ktor:ktor-client-core:1.6.7\")\n",
"@file:DependsOn(\"io.ktor:ktor-client-cio-jvm:1.6.7\")\n",
"@file:DependsOn(\"io.ktor:ktor-client-serialization-jvm:1.6.7\")\n",
"@file:DependsOn(\"ch.qos.logback:logback-classic:1.2.10\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Setting up data and logic"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"import kotlin.collections.sumOf\n",
"import kotlinx.serialization.Serializable\n",
"\n",
"@Serializable\n",
"data class Matrix(val array: DoubleArray, val rows: Int, val columns: Int){\n",
" init{ \n",
" require(array.size == rows*columns)\n",
" }\n",
" \n",
" operator fun get(row: Int, column: Int):Double = array[columns*row + column]\n",
" operator fun set(row: Int, column: Int, value: Double){ array[columns*row + column] = value}\n",
"}\n",
"\n",
"@Serializable\n",
"data class PairOfMatrices(val first: Matrix, val second: Matrix)\n",
"\n",
"infix fun Matrix.dot(other: Matrix): Matrix{\n",
" require(columns == other.rows)\n",
" \n",
" val res = Matrix(DoubleArray(rows*other.columns), rows, other.columns)\n",
" for(r in 0 until rows){\n",
" for(c in 0 until other.columns){\n",
" res[r, c] = (0 until columns).sumOf{ l -> get(r,l)*other.get(l,c) }\n",
" }\n",
" }\n",
" return res\n",
"}"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Setting up and stating server on localhost"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"datalore": {
"sheet_delimiter": false
}
},
"outputs": [],
"source": [
"import io.ktor.application.*\n",
"import io.ktor.response.*\n",
"import io.ktor.request.*\n",
"import io.ktor.routing.*\n",
"import io.ktor.http.*\n",
"import io.ktor.server.engine.*\n",
"import io.ktor.server.cio.*\n",
"import io.ktor.features.*\n",
"import io.ktor.serialization.*\n",
"\n",
"val server = embeddedServer(CIO, port = 7080) {\n",
" install(DefaultHeaders)\n",
" install(ContentNegotiation){\n",
" json()\n",
" }\n",
" \n",
" routing {\n",
" get(\"/\") {\n",
" call.respondText(\"Hello, world!\")\n",
" }\n",
" \n",
" post(\"/dot\"){\n",
" try{\n",
" val pair = call.receive<PairOfMatrices>()\n",
" val result = pair.first dot pair.second\n",
" call.respond(result)\n",
" } catch(t : Throwable){\n",
" call.respondText(\"error: $t\",status = HttpStatusCode.PreconditionFailed)\n",
" }\n",
" }\n",
" }\n",
"}.start(wait = false)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Setting up client for testing"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"import io.ktor.client.*\n",
"import io.ktor.client.features.json.*\n",
"\n",
"val client = HttpClient(){\n",
" install(JsonFeature)\n",
"}"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Matrix(array=[1.0, 0.5, 0.25, 0.5], rows=2, columns=2)\n"
]
}
],
"source": [
"import io.ktor.client.request.*\n",
"import io.ktor.client.statement.*\n",
"import io.ktor.http.*\n",
"\n",
"kotlinx.coroutines.runBlocking{\n",
" val product : Matrix = client.post(\"http://localhost:7080/dot\"){\n",
" contentType(ContentType.Application.Json)\n",
" body = PairOfMatrices(\n",
" Matrix(doubleArrayOf(1.0, 0.0, 0.0, 0.5),2,2),\n",
" Matrix(doubleArrayOf(1.0, 0.5, 0.5, 1.0),2,2),\n",
" )\n",
" }\n",
" println(product)\n",
"}"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {
"datalore": {
"sheet_delimiter": false
}
},
"outputs": [],
"source": [
"server.stop(1000,1000)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"datalore": {
"sheet_delimiter": false
}
},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Kotlin",
"language": "kotlin",
"name": "kotlin"
},
"language_info": {
"codemirror_mode": "text/x-kotlin",
"file_extension": ".kt",
"mimetype": "text/x-kotlin",
"name": "kotlin",
"nbconvert_exporter": "",
"pygments_lexer": "kotlin",
"version": "1.6.20-dev-6372"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment