srsadmm-core 0.1.2

Core library for the srsadmm project, used to solve consensus ADMM problems with serverless compute.
Documentation
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
use crate::{timing::TimingTracker, utils::LassoError};
use aws_sdk_lambda::Client as LambdaClient;
use aws_smithy_types::Blob;
use serde::{Deserialize, Serialize};
use std::{error::Error, fs::OpenOptions, io::Write, time::Instant};
use tokio::time::{sleep, Duration};

/// Payload for matrix multiplication Lambda function calls.
///
/// This structure contains the S3 object keys and bucket information
/// needed to perform distributed matrix multiplication via AWS Lambda.
#[derive(Serialize)]
pub struct MatrixMultiplicationPayload {
    /// S3 bucket containing the matrices
    pub bucket: String,
    /// S3 key for the first matrix operand
    pub matrix1_key: String,
    /// S3 key for the second matrix operand
    pub matrix2_key: String,
    /// S3 key where the result matrix should be stored
    pub out_key: String,
}

/// Payload for dual variable update Lambda function calls.
///
/// This structure contains the S3 object keys needed to perform
/// the dual update operation: y + x - z in distributed ADMM.
#[derive(Serialize)]
pub struct DualUpdatePayload {
    /// S3 bucket containing the vectors
    pub bucket: String,
    /// S3 key for the x vector
    pub x_key: String,
    /// S3 key for the z vector  
    pub z_key: String,
    /// S3 key for the y vector
    pub y_key: String,
    /// S3 key where the result should be stored
    pub out_key: String,
}

/// Payload for matrix factorization Lambda function calls.
///
/// This structure contains the information needed to compute
/// matrix factorizations required in ADMM algorithms.
#[derive(Serialize)]
pub struct FactorPayload {
    /// S3 bucket containing the matrix
    pub bucket: String,
    /// S3 key for the matrix to factorize
    pub a_key: String,
    /// S3 key where the factorization result should be stored
    pub out_key: String,
    /// Regularization parameter rho
    pub rho: f32,
}

/// Response structure from Lambda matrix operations.
///
/// All Lambda functions return this standardized response format
/// to indicate success or failure of the requested operation.
#[derive(Deserialize)]
pub struct MatrixOpResponse {
    /// Status of the operation ("success" or error message)
    pub status: String,
}

/// Calls the matrix multiplication Lambda function to multiply two matrices stored in S3
///
/// # Arguments
/// * `client` - The AWS Lambda client
/// * `bucket` - The S3 bucket name
/// * `matrix1_key` - The S3 key for the first matrix
/// * `matrix2_key` - The S3 key for the second matrix
/// * `out_key` - The S3 key where the result should be stored
///
/// # Returns
/// * `Result<(), Box<dyn Error>>` - Ok(()) if successful, error otherwise
pub async fn call_matrix_multiplication_lambda(
    client: &LambdaClient,
    bucket: &str,
    matrix1_key: &str,
    matrix2_key: &str,
    out_key: &str,
) -> Result<(), Box<dyn Error>> {
    const MAX_RETRIES: u32 = 3;
    let mut last_error = None;

    for attempt in 0..=MAX_RETRIES {
        let payload = MatrixMultiplicationPayload {
            bucket: bucket.to_string(),
            matrix1_key: matrix1_key.to_string(),
            matrix2_key: matrix2_key.to_string(),
            out_key: out_key.to_string(),
        };

        let payload = serde_json::to_string(&payload)?;
        
        match client
            .invoke()
            .function_name("srsadmm-lambda-mm")
            .payload(Blob::new(payload))
            .send()
            .await
        {
            Ok(response) => {
                let payload = response.payload().ok_or("Failed to get payload")?;
                let response_str = match std::str::from_utf8(payload.as_ref()) {
                    Ok(s) => s,
                    Err(e) => {
                        println!("[Lambda] Error converting payload to string: {}", e);
                        return Err(e.into());
                    }
                };
                
                match serde_json::from_str::<MatrixOpResponse>(response_str) {
                    Ok(matrix_response) => {
                        if matrix_response.status.to_lowercase() != "success" {
                            let error = format!("Lambda function failed: {}", matrix_response.status);
                            last_error = Some(error.clone());
                            
                            if attempt < MAX_RETRIES {
                                println!("[Lambda] Attempt {} failed: {}. Retrying...", attempt + 1, error);
                                // Exponential backoff: 1s, 2s, 4s
                                let delay = Duration::from_millis(1000 * (1 << attempt));
                                sleep(delay).await;
                                continue;
                            } else {
                                return Err(error.into());
                            }
                        } else {
                            // Success
                            if attempt > 0 {
                                println!("[Lambda] Matrix multiplication succeeded on attempt {}", attempt + 1);
                            }
                            return Ok(());
                        }
                    }
                    Err(e) => {
                        let error = format!("Failed to parse response: {}", e);
                        last_error = Some(error.clone());
                        
                        if attempt < MAX_RETRIES {
                            println!("[Lambda] Attempt {} failed: {}. Retrying...", attempt + 1, error);
                            let delay = Duration::from_millis(1000 * (1 << attempt));
                            sleep(delay).await;
                            continue;
                        } else {
                            return Err(error.into());
                        }
                    }
                }
            }
            Err(e) => {
                let error = format!("Lambda invocation failed: {}", e);
                last_error = Some(error.clone());
                
                if attempt < MAX_RETRIES {
                    println!("[Lambda] Attempt {} failed: {}. Retrying...", attempt + 1, error);
                    // Exponential backoff: 1s, 2s, 4s
                    let delay = Duration::from_millis(1000 * (1 << attempt));
                    sleep(delay).await;
                    continue;
                } else {
                    return Err(e.into());
                }
            }
        }
    }

    // This should never be reached, but just in case
    Err(last_error.unwrap_or_else(|| "Unknown error".to_string()).into())
}

/// Calls the matrix multiplication Lambda function with timing tracking
///
/// # Arguments
/// * `client` - The AWS Lambda client
/// * `tracker` - The timing tracker to record execution time
/// * `bucket` - The S3 bucket name
/// * `matrix1_key` - The S3 key for the first matrix
/// * `matrix2_key` - The S3 key for the second matrix
/// * `out_key` - The S3 key where the result should be stored
///
/// # Returns
/// * `Result<(), LassoError>` - Ok(()) if successful, error otherwise
pub async fn call_matrix_multiplication_lambda_timed(
    client: &LambdaClient,
    tracker: &mut TimingTracker,
    bucket: &str,
    matrix1_key: &str,
    matrix2_key: &str,
    out_key: &str,
) -> Result<(), LassoError> {
    let start = Instant::now();

    let result =
        call_matrix_multiplication_lambda(client, bucket, matrix1_key, matrix2_key, out_key).await;

    let duration = start.elapsed();
    tracker.record_lambda("srsadmm-lambda-gemm", "matrix_multiplication", duration);

    result
        .map_err(|e| LassoError::from_string(format!("Matrix multiplication lambda failed: {}", e)))
}

/// Logs lambda timing to a CSV file
pub(crate) fn log_lambda_timing(
    function_name: &str,
    operation_type: &str,
    duration_ms: f64,
    metadata: Option<&str>,
) {
    let log_entry = format!(
        "{},{},{:.3},{},{}\n",
        function_name,
        operation_type,
        duration_ms,
        std::time::SystemTime::now()
            .duration_since(std::time::UNIX_EPOCH)
            .unwrap()
            .as_secs(),
        metadata.unwrap_or("")
    );

    // Try to append to lambda_timings.csv, create if it doesn't exist
    if let Ok(mut file) = OpenOptions::new()
        .create(true)
        .append(true)
        .open("lambda_timings.csv")
    {
        // Write header if file is new (check file size)
        if file.metadata().map(|m| m.len()).unwrap_or(1) == 0 {
            let _ = writeln!(
                file,
                "function_name,operation_type,duration_ms,timestamp,metadata"
            );
        }
        let _ = write!(file, "{}", log_entry);
    }
}

/// Calls the matrix multiplication Lambda function with automatic timing logging
pub async fn call_matrix_multiplication_lambda_logged(
    client: &LambdaClient,
    bucket: &str,
    matrix1_key: &str,
    matrix2_key: &str,
    out_key: &str,
) -> Result<(), LassoError> {
    let start = Instant::now();

    let result =
        call_matrix_multiplication_lambda(client, bucket, matrix1_key, matrix2_key, out_key).await;

    let duration = start.elapsed();
    log_lambda_timing(
        "srsadmm-lambda-gemm",
        "matrix_multiplication",
        duration.as_secs_f64() * 1000.0,
        None,
    );

    result
        .map_err(|e| LassoError::from_string(format!("Matrix multiplication lambda failed: {}", e)))
}

/// Calls the matrix multiplication Lambda function with automatic timing logging and metadata
pub async fn call_matrix_multiplication_lambda_logged_with_metadata(
    client: &LambdaClient,
    bucket: &str,
    matrix1_key: &str,
    matrix2_key: &str,
    out_key: &str,
    matrix1_id: &str,
    matrix2_id: &str,
    out_id: &str,
) -> Result<(), LassoError> {
    let start = Instant::now();

    let result =
        call_matrix_multiplication_lambda(client, bucket, matrix1_key, matrix2_key, out_key).await;

    let duration = start.elapsed();
    let metadata = format!(
        "a_id:{}::b_id:{}::dest_id:{}",
        matrix1_id, matrix2_id, out_id
    );
    log_lambda_timing(
        "srsadmm-lambda-gemm",
        "matrix_multiplication",
        duration.as_secs_f64() * 1000.0,
        Some(&metadata),
    );

    result
        .map_err(|e| LassoError::from_string(format!("Matrix multiplication lambda failed: {}", e)))
}

pub async fn call_factor_lambda(
    client: &LambdaClient,
    bucket: &str,
    a_key: &str,
    out_key: &str,
    rho: f32,
) -> Result<(), Box<dyn Error>> {
    const MAX_RETRIES: u32 = 3;
    let mut last_error = None;

    for attempt in 0..=MAX_RETRIES {
        let payload = FactorPayload {
            bucket: bucket.to_string(),
            a_key: a_key.to_string(),
            out_key: out_key.to_string(),
            rho: rho,
        };

        let payload = serde_json::to_string(&payload)?;
        
        match client
            .invoke()
            .function_name("srsadmm-lasso-factor")
            .payload(Blob::new(payload))
            .send()
            .await
        {
            Ok(response) => {
                let payload = response.payload().ok_or("Failed to get payload")?;
                
                match std::str::from_utf8(payload.as_ref()) {
                    Ok(response_str) => {
                        match serde_json::from_str::<MatrixOpResponse>(response_str) {
                            Ok(matrix_response) => {
                                if matrix_response.status.to_lowercase() != "success" {
                                    let error = format!("Lambda function failed: {}", matrix_response.status);
                                    last_error = Some(error.clone());
                                    
                                    if attempt < MAX_RETRIES {
                                        println!("[Lambda] Attempt {} failed: {}. Retrying...", attempt + 1, error);
                                        // Exponential backoff: 1s, 2s, 4s
                                        let delay = Duration::from_millis(1000 * (1 << attempt));
                                        sleep(delay).await;
                                        continue;
                                    } else {
                                        return Err(error.into());
                                    }
                                } else {
                                    // Success
                                    if attempt > 0 {
                                        println!("[Lambda] Factor operation succeeded on attempt {}", attempt + 1);
                                    }
                                    return Ok(());
                                }
                            }
                            Err(e) => {
                                let error = format!("Failed to parse response: {}", e);
                                last_error = Some(error.clone());
                                
                                if attempt < MAX_RETRIES {
                                    println!("[Lambda] Attempt {} failed: {}. Retrying...", attempt + 1, error);
                                    let delay = Duration::from_millis(1000 * (1 << attempt));
                                    sleep(delay).await;
                                    continue;
                                } else {
                                    return Err(error.into());
                                }
                            }
                        }
                    }
                    Err(e) => {
                        let error = format!("Failed to convert response to string: {}", e);
                        last_error = Some(error.clone());
                        
                        if attempt < MAX_RETRIES {
                            println!("[Lambda] Attempt {} failed: {}. Retrying...", attempt + 1, error);
                            let delay = Duration::from_millis(1000 * (1 << attempt));
                            sleep(delay).await;
                            continue;
                        } else {
                            return Err(error.into());
                        }
                    }
                }
            }
            Err(e) => {
                let error = format!("Lambda invocation failed: {}", e);
                last_error = Some(error.clone());
                
                if attempt < MAX_RETRIES {
                    println!("[Lambda] Attempt {} failed: {}. Retrying...", attempt + 1, error);
                    // Exponential backoff: 1s, 2s, 4s
                    let delay = Duration::from_millis(1000 * (1 << attempt));
                    sleep(delay).await;
                    continue;
                } else {
                    return Err(e.into());
                }
            }
        }
    }

    // This should never be reached, but just in case
    Err(last_error.unwrap_or_else(|| "Unknown error".to_string()).into())
}

pub async fn call_factor_lambda_logged(
    client: &LambdaClient,
    bucket: &str,
    a_key: &str,
    out_key: &str,
    rho: f32,
) -> Result<(), LassoError> {
    let start = Instant::now();

    let result = call_factor_lambda(client, bucket, a_key, out_key, rho).await;

    let duration = start.elapsed();
    log_lambda_timing(
        "srsadmm-lambda-factor",
        "factor",
        duration.as_secs_f64() * 1000.0,
        None,
    );

    result.map_err(|e| LassoError::from_string(format!("Factor lambda failed: {}", e)))
}