aboutsummaryrefslogtreecommitdiff
path: root/platform/linux-generic/example/ml/model_read.c
blob: 7aa20bf3559caa50c15288db0b8c07b5c95e3a84 (plain)
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
/* SPDX-License-Identifier: BSD-3-Clause
 * Copyright (c) 2023 Nokia
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <odp_api.h>

#include "model_read.h"

int read_model_from_file(const char *file_name, odp_ml_model_param_t *model_param)
{
	FILE *model_file;
	/* Number of elements successfully read */
	size_t num_elem;

	/* Get the model file size in bytes */
	model_file = fopen(file_name, "rb");
	if (model_file == NULL) {
		perror("Failed to open model file");
		return -1;
	}

	fseek(model_file, 0, SEEK_END);
	model_param->size = ftell(model_file);
	rewind(model_file);

	/* Allocate memory for model buffer */
	model_param->model = malloc(model_param->size);
	memset(model_param->model, 0, model_param->size);
	if (!model_param->model) {
		printf("Allocating memory for model buffer failed\n");
		return -1;
	}

	/* Read the model file */
	num_elem = fread(model_param->model, model_param->size, 1, model_file);
	fclose(model_file);
	if (num_elem != 1) {
		printf("Read model file failed\n");
		free(model_param->model);
		return -1;
	}

	return 0;
}